Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Programically selecting an item in DataGridComboBoxCell
 

Programically selecting an item in DataGridComboBoxCell

I have a datagridviewComboBoxCell that each item has a unique id. How can a programically pick an item by that unique id and have it displayed in the cell?
Aarron345  Thursday, July 31, 2008 9:01 PM

Hi Aarron345,

Usually, we use DataGridViewComBoxCell to populate other fields of DataGridView, such as auto fill fields of other columns.

However, sometimes, we’d like to make it act more like a real ComBox which can display item directly, right.

Then I’d like to suggest you using DataGridView.EditingControlShowing event, in which you can change ComboBoxCell into ComboBox and set its SelectedItem. Please refer to the following code:

Code Snippet

private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (this.dataGridView2.CurrentCellAddress.X == 0)

{

ComboBox c = e.Control as ComboBox;

c.SelectedItem = c.Items[0];

}

}

Furthermore, because I cannot be sure about how you get “ID�and how you use it to find the match item, I’d like to provide some information about DisplayMember and ValueMember of ComboBoxCell.

We usually use ValueMember to implicitly indicate what value will be displayed for user. Note that ValueMember cannot be seen directly, we can only see its corresponding DisplayMember through UI. So, under this problem, we can set ValueMember to “ID�and set DisplayMember to corresponding field like “name�

For more information about DisplayMember, ValueMember and Value properties, please refer to the following pages:

DataGridViewComboBoxCell.ValueMember Property

DataGridViewComboBoxCell.DisplayMember Property

Please do not hesitate to let me know if there is still question.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Wednesday, August 06, 2008 7:22 AM

Hi Aarron345,

Usually, we use DataGridViewComBoxCell to populate other fields of DataGridView, such as auto fill fields of other columns.

However, sometimes, we’d like to make it act more like a real ComBox which can display item directly, right.

Then I’d like to suggest you using DataGridView.EditingControlShowing event, in which you can change ComboBoxCell into ComboBox and set its SelectedItem. Please refer to the following code:

Code Snippet

private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

if (this.dataGridView2.CurrentCellAddress.X == 0)

{

ComboBox c = e.Control as ComboBox;

c.SelectedItem = c.Items[0];

}

}

Furthermore, because I cannot be sure about how you get “ID�and how you use it to find the match item, I’d like to provide some information about DisplayMember and ValueMember of ComboBoxCell.

We usually use ValueMember to implicitly indicate what value will be displayed for user. Note that ValueMember cannot be seen directly, we can only see its corresponding DisplayMember through UI. So, under this problem, we can set ValueMember to “ID�and set DisplayMember to corresponding field like “name�

For more information about DisplayMember, ValueMember and Value properties, please refer to the following pages:

DataGridViewComboBoxCell.ValueMember Property

DataGridViewComboBoxCell.DisplayMember Property

Please do not hesitate to let me know if there is still question.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Wednesday, August 06, 2008 7:22 AM

You can use google to search for other answers

Custom Search

More Threads

• XML Web Services over APACHE ??
• Local Terrarium as Screensaver
• Source Code
• ¿Why TaskVision does NOT use DataBinding?
• Puzzled
• Do I hear C# anytime soon???
• Insttal problems
• Xpander controls license
• Permission Denied :'getObject'
• How to scroll the scroll bar of TreeVIew control automatically?