DataTable > DataView > ComboBox?
-
I wanted to bind the elements in a ComboBox to a column in a DataTable, but wanted them to be sorted. This application is targeted for the Compact Framework, and I didn't see a way to do that directly. I decided to create a DataView object from the DataTable with the particular sort I was looking for. I set the DataSource of the ComboBox to the DataView, and setup the Display and Value members as needed by the app. Everything seemed to be working as expected, but I have run into one snag I can't seem to solve. If I change the name of one of the colunmns in the DataTable (ultimately the DisplayMember in the ComboBox), it doesn't propogate to the ComboBox by way of the DataView. On the other hand if I add or delete a row to the DataTable, everything does propogate to the ComboBox, even the row where I only changed the name! Is this the expected behaviour? Is there something I can do to force the ComboBox to reload it's drop down list?
-
I wanted to bind the elements in a ComboBox to a column in a DataTable, but wanted them to be sorted. This application is targeted for the Compact Framework, and I didn't see a way to do that directly. I decided to create a DataView object from the DataTable with the particular sort I was looking for. I set the DataSource of the ComboBox to the DataView, and setup the Display and Value members as needed by the app. Everything seemed to be working as expected, but I have run into one snag I can't seem to solve. If I change the name of one of the colunmns in the DataTable (ultimately the DisplayMember in the ComboBox), it doesn't propogate to the ComboBox by way of the DataView. On the other hand if I add or delete a row to the DataTable, everything does propogate to the ComboBox, even the row where I only changed the name! Is this the expected behaviour? Is there something I can do to force the ComboBox to reload it's drop down list?
This is expected behaviour. Sadly enough there aren't any event handlers for changing column names in a DataTable object, and I believe they do not show up in the Changes collection either. You could try re-binding the combobox when a form or control based event fires. Torin Blair
'In the immortal words of Socrates - "I drank what?".' -
This is expected behaviour. Sadly enough there aren't any event handlers for changing column names in a DataTable object, and I believe they do not show up in the Changes collection either. You could try re-binding the combobox when a form or control based event fires. Torin Blair
'In the immortal words of Socrates - "I drank what?".'I ended up handling the Underlying DataTable RowChanged event, and as you suggested, re-bound the combobox to the DataView. It seemed so cumbersomb that I thought there must have been a better way. Thanks for the reply.
-
This is expected behaviour. Sadly enough there aren't any event handlers for changing column names in a DataTable object, and I believe they do not show up in the Changes collection either. You could try re-binding the combobox when a form or control based event fires. Torin Blair
'In the immortal words of Socrates - "I drank what?".'Oops... I hope I didn't mislead you... I'm not changing the column names, I'm changing the content of a column in one of the rows of the DataTable. When I do that, the new value doesn't propogate to the combobox. If I add a new row to the DataTable, everything propogates as I expected.