【问题标题】:DatagridVIew single cell formattingDatagridVIew 单个单元格格式
【发布时间】:2020-12-06 11:04:41
【问题描述】:

试图在这个问题上找到答案,但没有发现任何对我有用的东西。 我需要根据不同的条件更改 dataGridView 中单个单元格的字体颜色(背景颜色等)。Fof 示例 - 此 dataGridView 中同一行的另一个单元格中的值。 我之前找到的所有解决方案都不能为我解决这个问题。

以下是我解决此问题的建议。

【问题讨论】:

    标签: c# visual-studio winforms datagridview cell-formatting


    【解决方案1】:

    这是我的解决方案: 你需要创建事件datagridView_cell_Formating:

    并添加类似这样的代码:

     private void datagridView_cell_Formating(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Target_column")
            {
                if ((e.Value != null) && (dataGridView1.Rows[e.RowIndex].Cells["Condition_cell"].Value.ToString() == "value"))
                {
                    e.CellStyle.ForeColor = Color.Yellow;
                }
                else if ((e.Value != null))
                {
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }
    

    希望这会对某人有所帮助。

    【讨论】:

      【解决方案2】:

      每个 DataGridView 都有一个 DefaultCellStyle。 DataGridView 的每个DataGridViewColumn 都可能有一个DefaultCellStyle。如果这是 NULL,则使用 DataGridView 的 DefaultCellStyle。要查看列使用的实际单元格样式,请使用InheritedCellStyle

      同样,每个 DataGridViewCell 都有一个 Style。如果为空,则使用单元格列的InheritedCellStyle,因此如果此列有空DefaultCellStyle,,则使用DataGridViewDefaultCellStyle。 DataGridViewCell的实际使用的Style在属性InheritedCellStyle中。

      • 单元格具有非空样式:InheritedStyle 等于此
      • 单元格有空样式,列有非空 Defaultstyle:Cell.InheritedStyle 等于 Column DefaultStyle
      • 单元格样式为空,列为空 DefaultStyle:Cell.InheritedStyle 等于 DataGridview 样式

      如果你只想改变某个单元格X的单元格样式,只需设置DefaultCellStyle。您可以克隆 InheritedStyle 并更改属性。

      void SetCellBackColor(DataGridViewCell cell, Color color)
      {
          cell.Style = (DataGridViewCellStyle)(cell.InheritedStyle.Clone());
      
          // or slightly more efficient:
          if (cell.Style == null)
             cell.Style = (DataGridViewCellStyle)(cell.InheritedStyle.Clone());
      
          cell.Style.BackColor = color;
      }
      void SetCellBackColorToDefault(DataGridViewCell cell)
      {
          // set Style to null; the Column's InheritedStyle will be used
          cell.Style = null;
      }
      

      【讨论】:

      • 做一些调试,写一些测试代码。您的调试器对单元格的 InheritedStyle.BackColor 有什么看法?它是否等于单元格的 Style.BackColor? HasStyle 是怎么说的?制作一些测试代码,其中 DataGridview / Column / Cell 每个都有不同的背景颜色,这样你就可以看到它显示的是哪个 BackColor:DataGridView 的一个,Column 的一个,还是 Cell 的一个?
      • 所以代码确实改变了继承的样式。继承样式的颜色是什么:是刚刚设置的Cell Style的颜色,还是列的颜色,还是gridview的颜色。顺便说一句:你知道像 GdiCharSet 和 vertical fornt 之类的东西在这个讨论中并不重要,请从 cmets 中删除它们。甚至更好:编辑您的原始问题,以便您可以进行一些正确的格式化。现在我几乎看不懂了。你读过关于样式和继承样式的 MSDN 吗?
      • 是的,你是对的。我还没有读过它,但这将是我接下来要学习的东西。我认为它应该对那些寻求解决方案的人有用。
      【解决方案3】:
             protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
              {
               if (e.Row.RowType == DataControlRowType.DataRow)
              {
              DataRowView dr = (DataRowView)e.Row.DataItem;
              string temp = dr[3].ToString().Trim();
              int sub = int.Parse(temp);
      
                 foreach (TableCell cell in e.Row.Cells)
                  {
                     if (sub > 0)
                     {
                      e.Row.Cells[3].BackColor = Color.Coral;
                      }
                      if (sub > 25)
                       {
                        e.Row.Cells[5].BackColor = Color.Coral;
                       }
                     }
                 }
            }
      

      如果这没有帮助,请详细说明要求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 2012-08-04
        相关资源
        最近更新 更多