( .NET 2.0, System.Windows.FormsDataGridView and DataTable )
I have 1 datagrid connected to 1 datatable.
That datatable contains 1 column: containing objects of my own type ‘MyObject’.
MyObject has a public string property ‘MyProp’.
I want to display 1 column in the grid: showing the string values of the property MyProp of the MyObjects in the table.
But I can’t seem to get it to work.
Schematically:
- myTable.Column1.Name: ‘Obj’
- myDataSet contains 1 table: myTable (filled with a few rows)
- myBindingSource.datasource = myDataSet
- myBindingSource.DataMember = myTable
- myDataGridView.DataSource = myBindingSource
-
in myDataGridView: 1 DataGridViewTextBoxColumn
-
tryout 1:
Column.DataPropertyName=’Obj’ (generated by default)
Displays the result of (overridden) MyObject.ToString() Not really what I want, this function is in use for logging.
- tryout 2:
GridViewColumn.DataPropertyName=’Obj.MyProp’
Doesn’t display anything (MyProp’s get is never called)
-
tryout 3: Made the MyProp property bindable:
[Bindable (BindableSupport.Yes)] public string MyProp { ...Samesame, not different from tryout 2
In short:
GridViewColumn.DataPropertyName doesn’t seem to support drilling further down into objects in a datatable’s column.
Anyone any ideas?
Thanks in advance!
Jan
Try just putting the Obj.MyProp string value into the datatable row. Putting an object into there will require it to be converted into a string representation (Obj.ToString()) which is getting you the output you are seeing.
A datatable is much like a spreadsheet, not like an Array(Of objects). You have to treat it as such,.