0

I have a DataGridView, with several ComboBoxColumns in it. Is there a way to create an event, so that each time a ComboBoxColumncell is entered and an item selected, the event fires? All I can figure out so far is this:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        MessageBox.Show("Amanda");
    }
}

Which is not doing anything.

Amanda_Panda
  • 1,156
  • 4
  • 26
  • 68

2 Answers2

0

You're probably looking for the EditingControlShowing event.

See here for a similar question: "SelectedIndexChanged" event in ComboBoxColumn on Datagridview

Community
  • 1
  • 1
Origin
  • 1,943
  • 13
  • 33
0

try this one.

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox nameComboBox = e.Control as ComboBox;
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                if (nameComboBox != null)
                {
                    ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                    ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                    ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;

                nameComboBox .SelectedIndexChanged -= (nameComboBox _SelectedIndexChanged);
                nameComboBox .SelectedIndexChanged += (nameComboBox _SelectedIndexChanged);

                }
            }
        }

    private void nameComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 1)
        {
        var rowindex = dataGridView1.CurrentCell.RowIndex;

              if (dataGridView1[1, rowindex].EditedFormattedValue != null)
              {
                  Consol.WriteLine(dataGridView1[1, rowindex].EditedFormattedValue.ToString());
              }
              else
              {
                 //No value in cell
              }
        }
    }
spajce
  • 7,044
  • 5
  • 29
  • 44
  • I literally copied and pasted that in, and it did nothing when I inserted a message box in to test. – Amanda_Panda Dec 07 '12 at 17:47
  • Basically, I'm trying to move a row from one dataGridView to another dataGridView when an item is selected from the comboboxcell. Well, there are four comboboxcolumns, but I figure I need to get the logic down for just one column first. – Amanda_Panda Dec 07 '12 at 17:54
  • i apologize the code is did nothing for you, but the code will trigger if the `Cell` is dirty it means you need to leave the `Cell` to validate your name if exist or not :) – spajce Dec 07 '12 at 21:38
  • hello @Amanda_Panda, i modified my code. try it. hope it will help :) – spajce Dec 07 '12 at 21:51