David N.4117


Hi All,

Is there a way to specify display value and actual (data) value for a DataGridViewComboBoxColumn

For example, my database table has a column account_type of type NCHAR(1), which is used to store either 'C' for Credit or 'D' for debit.  When the data records are retrieved from the table and displayed on a DataGridViewComboBoxColumn cell, I want it to be displayed as "Credit" or "Debit" respectively.  Vise-versa, when users add a new record, they should be able to select Credit or Debit from the combo box, but the actual data to be saved should be 'C' or 'D' respectively.

Thanks in advance




Re: Windows Forms General DataGridViewComboBoxColumn - Display vs. Actual (data) value

Mark Rideout

Yes - the DataGridViewComboBoxColumn supports the standard DisplayMember and ValueMember syntax. When you databind the combobox column just set the DisplayMember property to the string that identifies the account_type value and the ValueMember to the string that identifies the display value.

If you are not databinding the combobox column (setting the column's DataSource property) then you follow the same model except that you need to add objects directly to the combobox's Items collection. The objects you add is your own class - call it the Account class - that has a property for the actual value and a property for the display value. Check out the documentat for the standard ComboBox class (not the DGV version) for some more info on this.

Hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"




Re: Windows Forms General DataGridViewComboBoxColumn - Display vs. Actual (data) value

John RT F

Hello,

I have been following the example for using a DataGridViewComboBoxColumn from http://msdn2.microsoft.com/en-US/library/ms404353.aspx#Mtps_DropDownFilterText for an unbound grid but I am still having a problem.

Having populated the column.items with objects, and set the ValueMember and DisplayMember, the column correctly shows the pulldown list for a new row. The problem I am having is when I attempt to assign a value programmatically to a cell of the column.

I've tried assigning a string to the cell value, creating a new object with the same values as one item in the items list but both cause errors. How do I do this I can't seem to look up the item in the items list by displaymember string value, either.

John.





Re: Windows Forms General DataGridViewComboBoxColumn - Display vs. Actual (data) value

Price Brattin

I am trying to get this to work but do not understand how the DisplayMember could be bound to anything. The guy has a table that has a column (which I will call DrCr) that has values of "D" and "C". As I understand his objective (which is the same as mine), if the data has a value of "D" he wants to show "Debit" and when the data value is "C" he wants to show "Credit". He could bind the DrCr column to the ValueMember property but what would he bind to the DisplayMember property There is no table or column that shows "Debit" or "Credit". Those words appear only on the DGV. I assume he could use the Items collection in the Edit Columns wizard but how does he tell it to show "Credit" when the value of the data item is "C"