L
Lucvdv
[Using VB.Net 2003]
When filling a DataTable, I'm feeding instances of a custom class into one
of its columns, the way you would do with a ListBox for example.
My problem is that even though it accepts data of generic type "object", it
only seems to store a string representation in the DataTable.
"SettingItem" below is a custom class.
With a ListBox this works:
Dim lstTest As New ListBox
Dim x As New SettingItem
lstTest.Items.Add(x)
Debug.WriteLine(lstTest.Items(0).GetType)
It prints "ApplicatiunName.SettingItem".
Later, you can access the full selected SettingItem object in the listbox's
events. You control what is displayed in the listbox by overriding the
ToString method of whatever class you fill it with.
The Item property of a DataRow in a DataTable has "object" as type, so I
expected it to work the same way.
Dim dtSettings As New DataTable("Settings")
Dim dr As DataRow = dtSettings.NewRow
Dim ds As New SettingItem
...
dr.Item("Value") = ds
dtSettings.Rows.Add(dr)
Debug.WriteLine(dtSettings.Rows(0).Item("Value").GetType)
prints "System.String".
The DataTable is used as data source for a DataGrid control, and I created
a custom column editor derived from DataGridColumnStyle to edit the values.
There, the GetColumnValueAtRow method still returns an "object", and I need
it to give access to the original object that was stored in the DataTable.
Can this be done, or is there a simple workaround?
When filling a DataTable, I'm feeding instances of a custom class into one
of its columns, the way you would do with a ListBox for example.
My problem is that even though it accepts data of generic type "object", it
only seems to store a string representation in the DataTable.
"SettingItem" below is a custom class.
With a ListBox this works:
Dim lstTest As New ListBox
Dim x As New SettingItem
lstTest.Items.Add(x)
Debug.WriteLine(lstTest.Items(0).GetType)
It prints "ApplicatiunName.SettingItem".
Later, you can access the full selected SettingItem object in the listbox's
events. You control what is displayed in the listbox by overriding the
ToString method of whatever class you fill it with.
The Item property of a DataRow in a DataTable has "object" as type, so I
expected it to work the same way.
Dim dtSettings As New DataTable("Settings")
Dim dr As DataRow = dtSettings.NewRow
Dim ds As New SettingItem
...
dr.Item("Value") = ds
dtSettings.Rows.Add(dr)
Debug.WriteLine(dtSettings.Rows(0).Item("Value").GetType)
prints "System.String".
The DataTable is used as data source for a DataGrid control, and I created
a custom column editor derived from DataGridColumnStyle to edit the values.
There, the GetColumnValueAtRow method still returns an "object", and I need
it to give access to the original object that was stored in the DataTable.
Can this be done, or is there a simple workaround?