Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created comboBox column in my DataGridView
as
DataGridViewCell ComboCell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.AutoComplete = true;



comboCol.CellTemplate = ComboCell;
comboCol.DisplayMember = "MenuName";
comboCol.ValueMember = "MenuID";
comboCol.HeaderText = "Menu Name";
comboCol.DataSource = dealList;
comboCol.Name = "MenuName";
comboCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.Width = 120;
Grd.Columns.Add(comboCol);


Now i want if the index in the combobox is changed in this column say "MenuName" then i can assign the selected value to another column say "MenuID"
how can i do this?
Please guide me... Thank you in advance
Posted

1 solution

Hello WaseemAmin,

Unfortunately the DataGridViewComboBoxColumn doesn't expose a SelectedIndexChanged Event. In order to catch an index change of a ComboBoxColumn, you 'll first need to cast the cell to a DataGridViewComboBoxEditingControl and Add an EventHandler to that control. This you will have to do in the EditingControlShowing Event of the DataGridView .

suppose i have a datagridview named as dataGridViewSales .and in the datagridview i have one combobox . Now i want to change the datagrid view second column value according to combobox selected value.

C#
private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           ComboBox cb = e.Control as ComboBox;
           if (cb != null)
           {
               cb.SelectedIndexChanged += new EventHandler(selectionchange);
              //here SelectedIndexChanged event is used
           }
       }


and then define selectionchange method

C#
void selectionchange(object sender, EventArgs e)
       {
           try
           {
               ComboBox cb = (ComboBox)sender;
               if (cb.Text == "yes")
               {
                   dataGridViewSales.CurrentRow.Cells[1].Value= "Hello";
//if datagridviewcombobox text is yes then set the first column value to 'Hello'
               }
               else if (cb.Text == "no")
               {
                   dataGridViewSales.CurrentRow.Cells[1].Value = string.Empty;
 //else set the first column  value to empty
               }
           }
           catch { }
       }


thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900