【问题标题】:Change the back color of the cell for DataGridViewComboBoxColumn更改 DataGridViewComboBoxColumn 单元格的背景颜色
【发布时间】:2015-08-10 13:25:28
【问题描述】:

我创建了一个 DataGridView 对象,其列类型为 DataGridViewComboBoxColumn,以允许用户从下拉列表中选择值。例如,如果用户选择“高”,我想为组合框的背面着色。但是,它不会为组合框着色,而只会为组合框的值着色。

代码是:

dgvOverallRisk.Rows[0].Cells[1].Style.ForeColor = Color.Aqua;
dgvOverallRisk.Rows[0].Cells[1].Style.BackColor = Color.Red;

它看起来像这样:

【问题讨论】:

  • 我相信你做不到。蓝色是系统颜色,因此您唯一的选择是更改该颜色。但是,这样做也会改变系统中所有所选项目的颜色。

标签: c# winforms datagridview


【解决方案1】:

巧合的是,我刚刚在ForeColor 找到了here 回答了一个类似的问题。本质上是一样的,你有两种选择:

  1. 设置FlatStyle。但它可能看起来不像你想要的那样。

    theComboBoxColumn.FlatStyle = FlatStyle.Flat;
    
  2. 这对你来说不是一个确切的解决方案,因为我在 Windows 8.1 上并且 从您的屏幕截图来看,您使用的是 Windows 7。他们有 ComboBox 控件上的显示样式不同,但这 应该让您大致了解可能的方向 拿。处理DataGridView.CellPaintingDataGridView.EditingControlShowing 事件以手动绘制ComboBox 单元格。

    this.dataGridView1.CellPainting += this.dataGridView1_CellPainting;
    this.dataGridView1.EditingControlShowing += this.dataGridView1_EditingControlShowing;
    
    this.dataGridView1.Rows[0].Cells[1].Style.ForeColor = Color.DarkRed;
    this.dataGridView1.Rows[0].Cells[1].Style.BackColor = Color.Bisque;
    this.dataGridView1.Rows[1].Cells[1].Style.ForeColor = Color.SpringGreen;
    this.dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Purple;
    
    // Paint the cell when not in edit mode.
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
      if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
      {
        if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewComboBoxCell)
        {
          var cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
          var foreColor = cell.Style.ForeColor.Name == "0" ? Color.Black : cell.Style.ForeColor;

          e.Paint(e.ClipBounds, DataGridViewPaintParts.Border);
          e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentBackground);

          using (Brush forebrush = new SolidBrush(foreColor))
          using (Brush backbrush = new SolidBrush(cell.Style.BackColor))
          using (StringFormat format = new StringFormat())
          {
            Rectangle rect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 19, e.CellBounds.Height - 3);
            format.LineAlignment = StringAlignment.Center;

            e.Graphics.FillRectangle(backbrush, rect);
            e.Graphics.DrawString(cell.FormattedValue.ToString(), e.CellStyle.Font, forebrush, rect, format); 
          }

          e.Paint(e.ClipBounds, DataGridViewPaintParts.ErrorIcon);
          e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);
          e.Handled = true;
        }
      }
    }

    // Paint the cell in edit mode.
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
      if (this.dataGridView1.CurrentCellAddress.X == combo.DisplayIndex)
      {
        ComboBox cb = e.Control as ComboBox;
        if (cb != null)
        {
          cb.DropDownStyle = ComboBoxStyle.DropDownList;

          cb.DrawMode = DrawMode.OwnerDrawFixed;
          cb.DrawItem -= this.cb_DrawItem;
          cb.DrawItem += this.cb_DrawItem;
        }
      }
    }

    // Manually paint the combobox.
    private void cb_DrawItem(object sender, DrawItemEventArgs e)
    {
      ComboBox cb = sender as ComboBox;

      // Non-sourced vs sourced examples.
      string value = cb.Items[e.Index].ToString();
      // string value = (cb.DataSource as DataTable).Rows[e.Index].ItemArray[SourceColumnIndex];

      if (e.Index >= 0)
      {
        using (Brush forebrush = new SolidBrush(cb.ForeColor))
        using (Brush backbrush = new SolidBrush(cb.BackColor))
        {
          e.Graphics.FillRectangle(backbrush, e.Bounds);
          e.DrawBackground();
          e.DrawFocusRectangle();
          e.Graphics.DrawString(value, e.Font, forebrush, e.Bounds);
        }
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2013-04-12
    • 2021-10-31
    • 2016-05-27
    • 2013-06-15
    • 2013-06-28
    • 2020-02-02
    相关资源
    最近更新 更多