Windows Develop Bookmark and Share   
 index > Windows Forms Designer > set selected index of combobox column in datagridview
 

set selected index of combobox column in datagridview

Hi all


I am binding the comboboxcolumn of the datagridview like

DataGridViewComboBoxCell obj = new DataGridViewComboBoxCell();
row.Cells[4] = obj;
DataView dv = new DataView(DTBankAccounts, "fundId = " + fundid, "", DataViewRowState.CurrentRows);
obj.DisplayMember = "accountID";
obj.ValueMember = "accountID";
obj.DataSource = dv;

the combobox when loads shows empty,when i click the combobox i can see the items to select ,How to set the default selected index to 0


Thanks in advance
DAM
damu maddy  Friday, January 16, 2009 6:34 PM

Hi damu maddy,

ComboBoxCell doesn't support SelectedIndex property, I have found two way to achieve.
1. Set the initial value of the ComboBoxCell.
For example, here is a table with CountryID and CountryName
private DataTable dtCountry = new DataTable();
dtCountry.Columns.Add("CountryID", typeof(int));
dtCountry.Columns.Add("CountryName", typeof(string));
dtCountry.Rows.Add(1, "Canada");
dtCountry.Rows.Add(2, "USA");
dtCountry.Rows.Add(3, "Mexico");

I will create a new DataGridViewComboBoxCell instance and set its initial value with the first row in dtCountry.
DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell();
dgvCmbCell.DataSource = dtCountry;
dgvCmbCell.DisplayMember = "CountryName";
dgvCmbCell.ValueMember = "CountryID";
// use this cell on row[0] cell[1]
dataGridView1.Rows[0].Cells[1] = dgvCmbCell;
if (dataGridView1.Rows[0].Cells[1].Value.ToString() == "")
{
dgvCmbCell.Value = dtCountry.Rows[0]["CountryID"];
}

2. Set the SelectedIndex to 0 when you click the cell and want to edit it. So you need to handleEditingControlShowing event.
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmb = e.Control as ComboBox;
if (cmb != null)
{
cmb.SelectedIndex = 0;
}
}

Holp this can help you!
If you have any question, please tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer bydamu maddy Thursday, January 22, 2009 8:52 PM
  •  
Kira Qian  Wednesday, January 21, 2009 3:08 AM

Hi damu maddy,

DataGridViewComboBoxCell doesn't support default value property. If you want to let the undecided cell select the index 0, you can use the DefaultValue of DataColumn. A default value is the value that is automatically assigned to the column when a DataRow is created.

DataColumn.DefaultValue Property
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.defaultvalue.aspx

For example.
// dtSource is the datasource that will be bound to your DataGridView.
DataTable dtSourec = new DataTable();

// Set AccoundID column DefaultValue property to 1,
dtSource.Columns["AccoundID"].DefaultValue = 1;

Does this can solve your problem. If I have something wrong, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, January 20, 2009 4:24 AM
Hi Kiran Qian


Thanks for the reply

I want to show the dynamic values coming from database ,for ex
My combobox column have three items like item 1,item2,item 3 which i dynamically bind

When the datagridview is loaded a empty combobox shows and when i pull the dropdown i can see the items to select ,I want the display the firstitem in the comboboxccell when the datagridview gets loaded like a selected index property in a normal combobox


cheers
damodar
damu maddy  Tuesday, January 20, 2009 2:25 PM

Hi damu maddy,

ComboBoxCell doesn't support SelectedIndex property, I have found two way to achieve.
1. Set the initial value of the ComboBoxCell.
For example, here is a table with CountryID and CountryName
private DataTable dtCountry = new DataTable();
dtCountry.Columns.Add("CountryID", typeof(int));
dtCountry.Columns.Add("CountryName", typeof(string));
dtCountry.Rows.Add(1, "Canada");
dtCountry.Rows.Add(2, "USA");
dtCountry.Rows.Add(3, "Mexico");

I will create a new DataGridViewComboBoxCell instance and set its initial value with the first row in dtCountry.
DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell();
dgvCmbCell.DataSource = dtCountry;
dgvCmbCell.DisplayMember = "CountryName";
dgvCmbCell.ValueMember = "CountryID";
// use this cell on row[0] cell[1]
dataGridView1.Rows[0].Cells[1] = dgvCmbCell;
if (dataGridView1.Rows[0].Cells[1].Value.ToString() == "")
{
dgvCmbCell.Value = dtCountry.Rows[0]["CountryID"];
}

2. Set the SelectedIndex to 0 when you click the cell and want to edit it. So you need to handleEditingControlShowing event.
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmb = e.Control as ComboBox;
if (cmb != null)
{
cmb.SelectedIndex = 0;
}
}

Holp this can help you!
If you have any question, please tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer bydamu maddy Thursday, January 22, 2009 8:52 PM
  •  
Kira Qian  Wednesday, January 21, 2009 3:08 AM
Great Kira Qian i solved my issue

Many thanks

dam
damu maddy  Thursday, January 22, 2009 8:53 PM

You can use google to search for other answers

Custom Search

More Threads

• Outlook Style Custom List Control
• Remove default ToolStripItems from ToolStrip Designer
• List type property, designer update problem
• How can I write code/event code for ToolBar Button individually?
• Controls inside a control being added twice
• Slow form loading time
• Good way to expose properties of members in the property grid?
• treeview scroll position
• PRB - SplitContainerDesigner initialization throw exception.
• How do I unassociate the designer from a class?