Click here to Skip to main content
16,022,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to handle selectedIndexChanged Event of datagridview combobox cell
Posted
Comments
CHill60 7-Feb-13 6:24am    
What have you tried? What do you want to happen in this event?
Ravndra22 7-Feb-13 6:30am    
I have 3 columns, first and 3rd as text column and 2nd as combobox column.
At index changed event of combobox,I want to fill 3 rd column based on combobox index value

Yes I got the answer

ComboBox combo = e.Control as ComboBox;
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
combo.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);
combo.SelectedIndexChanged+=new EventHandler(combo_SelectedIndexChanged);
}
void combo_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string selectedValue = ((ComboBox)sender).Text;

DataTable tasklist1 = new DataTable();

mtr.itemName = selectedValue;
tasklist1 = mtr.GetJobItemHrs();
if (tasklist1.Rows.Count > 0)
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Value = tasklist1.Rows[0][0].ToString();

}
}
catch
{ }

}

its working properly
 
Share this answer
 
Hows this:


C#
DataGridView dgv = new DataGridView();

public ctor(object sender, EventArgs e)
{
    //triggered whenever a cell is edited
    dgv.EditingControlShowing += DgvEditingControlShowing;
}

void DgvEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox)
    {
        ComboBox comboBox = e.Control as ComboBox;
        //now we have a combobox, lets get the row
        comboBox.SelectedIndexChanged += delegate
          {
             DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)sender;
             DataGridViewRow row = dgv.Rows[cell.RowIndex];
             row.Cells[3].Value = comboBox.SelectedText;
          };
    }
}


I haven't tested this code but you should be able to reverse engineer it to your needs
 
Share this answer
 
Comments
Ravndra22 7-Feb-13 22:49pm    
I want to retrive valueas from database based on combobox cell. And then fed it into textbox cell.For this above code is not going well..... :-(

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