Search code examples
c#.netwinformsdatagridviewdatagridcomboboxcolumn

ComboBox in DataGridView changes colors when I leave cell


I am currently trying to get ComboBoxes inside of my DataGridView to change color once their selected indices have been changed. I can get them to successfully change color from white to yellow when the cell's selected index changes, but when I leave the cell it turns white again. I have no idea why it is doing this.

The code provided below is from my class which inherits from DataGridView.

I add the EditingControlShowing listener in my constructor:

  this.columnCardName = new System.Windows.Forms.DataGridViewTextBoxColumn();
  this.columnCardNumber = new System.Windows.Forms.DataGridViewTextBoxColumn();
  this.extraColumns = new List<DataGridViewColumn>();
  this.blocks = blocks;
  this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  this.Location = new System.Drawing.Point(81, 54);
  this.Name = "dataGridView1";
  this.RowTemplate.Height = 24;
  this.Size = new System.Drawing.Size(1589, 934);
  this.TabIndex = 0;
  this.EditingControlShowing += dataGridView_EditingControlShowing; // This is where I add the event handler

This is the code that I use to generate the columns with the ComboBoxes:

  DataGridViewComboBoxColumn column = new System.Windows.Forms.DataGridViewComboBoxColumn();
  column.DropDownWidth = SMALL_COLUMN_WIDTH;
  column.HeaderText = columnHeader;
  column.Name = columnName;
  column.DropDownWidth = 160;
  column.Width = SMALL_COLUMN_WIDTH;
  DataTable data = new DataTable();
  data.Columns.Add(new DataColumn("Value", typeof(string)));
  data.Rows.Add("N");
  data.Rows.Add("M");
  data.Rows.Add("Q");
  column.DataSource = data;
  column.ValueMember = "Value";
  this.Columns.Add(column);

And finally, these are my handlers for the EditingControlShowing and SelectedIndexChanged:

private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
  if (e.Control is ComboBox) {
    ((ComboBox)e.Control).SelectedIndexChanged -= new EventHandler(comboBoxEventHandler);
    ((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(comboBoxEventHandler);
  }
}

private void comboBoxEventHandler(object sender, EventArgs e) {
  Console.WriteLine("Event firing "+e.GetType());
  ((ComboBox)sender).BackColor = System.Drawing.Color.Yellow;
}

Solution

  • try the following code should work:

      DataGridViewComboBoxColumn myCombo = new DataGridViewComboBoxColumn();
      dataGridView1.DataSource = dataSetFromDatabaseCall.Tables[0];
      myCombo.HeaderText = "My Combo";
      myCombo.Name = "myCombo";
      this.dataGridView1.Columns.Insert(1, myCombo);
    
      myCombo.Items.Add("test1");
      myCombo.Items.Add("test2");
      myCombo.Items.Add("test3");
    
    
    
     //event to check the cell value changed
     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
            if (e.ColumnIndex == myCombo.Index && e.RowIndex >= 0) //check if it is the combobox column
            {
                dataGridView1.CurrentCell.Style.BackColor = System.Drawing.Color.Yellow;
            }
     }
    
     private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
     {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
     }