【问题标题】:ComboBox in DataGridView changes colors when I leave cell当我离开单元格时,DataGridView 中的 ComboBox 会更改颜色
【发布时间】:2014-06-25 19:05:25
【问题描述】:

我目前正在尝试在我的 DataGridView 中获取 ComboBoxes,以便在更改所选索引后更改颜色。当单元格的选定索引发生变化时,我可以让它们成功地将颜色从白色变为黄色,但是当我离开单元格时,它再次变为白色。我不知道它为什么这样做。

下面提供的代码来自我的类​​,它继承自 DataGridView。

我在构造函数中添加了 EditingControlShowing 侦听器:

  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

这是我使用组合框生成列的代码:

  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);

最后,这些是我的 EditingControlShowing 和 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;
}

【问题讨论】:

  • 如果不查看您的代码,听起来您的 DefaultCellStyle for SelectionBackColorSelectionForeColor 与您的 BackColorForeColor 不同。因此,在您的代码中添加DataGridView.DefaultCellStyle.SelectionBackColor = DataGridView.DefaultCellStyle.BackColorForeColor 的相同

标签: c# .net winforms datagridview datagridcomboboxcolumn


【解决方案1】:

尝试以下代码应该可以工作:

  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);
        }
 }

【讨论】:

  • 这让我到达了我需要去的地方。谢谢。
猜你喜欢
  • 2017-02-05
  • 2013-04-12
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
相关资源
最近更新 更多