Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridViewComboBoxColumn - Display vs. Actual (data) value
 

DataGridViewComboBoxColumn - Display vs. Actual (data) value


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

David N_2  Friday, May 20, 2005 5:45 AM
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"
Mark Rideout  Wednesday, September 07, 2005 8:45 PM
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"
Mark Rideout  Wednesday, September 07, 2005 8:45 PM

Hello,

I have been following the example for using a DataGridViewComboBoxColumn from http://msdn2.microsoft.com/en-US/library/ms404353.aspx#Mtps_DropDownFilterTextfor 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.

John RT F  Friday, May 26, 2006 3:06 AM
I am trying to get this to work butdo 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"?
Price Brattin  Monday, June 25, 2007 10:38 PM

You can use google to search for other answers

Custom Search

More Threads

• Try powerful .NET reporting on line!
• IDE will not obey they settings for form height and width
• Why is the "Combobox" behavior different when setting focus to different control in Windows Vista and XP?
• Combobox with two columns
• Auto-register new type librarys
• How can I resize my TrackBarToolStripButton
• Putting classes using MS Office into the Global Assembly Cache
• How to make a multiline MessageBox?
• Mousedown Event on ToolStripButton Control Collection
• Passing Data between EXEs