I have a DataGridView bound to a DataTable. Once the binding is done, I manually search through all the cells of the DGV and replace a single regular DGV cell with a DataGridViewComboBoxCell instance. The cell has its own data source bound to it.
The problem I'm having is that when the user makes 开发者_JAVA技巧a selection in the DataGridViewComboBoxCell cell and leaves it, it changes its displayed string from what the DisplayMember is (something like "Warehouse #23") to its underlying ValueMember (something like 23). When the cell is re-entered, it switches back to displaying the DisplayMember (and so on). Other than this, everything functions correctly (the GUI is usable, selections can be made, only displaying is faulty).
Is there a property on the DataGridViewComboBoxCell object that controls this behaviour? Or does this behaviour come from me manually implanting a DataGridViewComboBoxCell in a regular DGV column?
Oh and the reason I'm doing this is that I need a simple way of constructing an on-the-fly user-input GUI. One can simply construct a DataTable programatically and plug it into the DGV. DGV constructs "the GUI" and makes it usable. You can use one column for a header and one for user's input. I now need not just standard text cells (which are basically textboxes), but selection-oriented combo-box cells as well.
I'll provide a solution of sorts for people that might find themselves in this situation.
I did not discover any real cause of what's going on, but managed to fix the situation by handling the display of cells contents manually. You can do that by implementing an event handler for the CellFormatting event of the DataGridView. There, you set the e.Value property to whatever you need displayed.
精彩评论