以编程方式更改数据网格视图中的颜色单元格

本文关键字:视图 颜色 单元格 网格 数据网 编程 方式更 数据 | 更新日期: 2023-09-27 18:35:43

我尝试改变DataGridView单元格的颜色。

我对datagridview有点陌生,并且已经查看了此处发布的许多问题,但似乎没有一个对我有用。

我的最后一个迭代(仍然不起作用)是:

DataGridView1.AutoGenerateColumns = true;
        DataGridView1.AutoSize = true;
        DataGridView1.DataSource = DT;
        int rowIndex = 0;
        int cellIndex = 0;
        Color c = Color.Gray;
        foreach(var row in  DataGridView1.Rows.Cast<DataGridViewRow>().ToList())//.ForEach(row =>
        {
            var cells = row.Cells;
            if (cells.Count > 0) {
                cellIndex = 0;
                foreach (DataGridViewCell cell in cells)
                {
                    DataGridView1.Rows[rowIndex].Cells[cellIndex].Style.BackColor = c;
                    //cell.Style.BackColor = c;
                    cellIndex++;
                }
                if (cells[0].Value!= null)
                if ((cells[0].Value as string ).Contains("==="))/*end of section*/
                {
                    c = (c == Color.Gray) ? Color.Transparent : Color.Gray;/*change color at end of section*/
                }
            }
            rowIndex++;
        }
        rowIndex = 0;

我的代码可能有什么问题?是否需要在 True''False 上设置任何参数才能更改颜色?

编辑我做什么:我想打开只有DataGridView的新表单,所以数据和所有其他用途都带有上下文菜单(MouseButtons.Right

更多代码

public partial class ResultsDiffForm : Form 
{
    public ResultsDiffForm(DataTable DT)//consatructor
    {
        InitializeComponent();
        /*Old code goes here ... */
    }
}

应该更清楚我在做什么。

以编程方式更改数据网格视图中的颜色单元格

您没有在调用此代码的位置发布,但是当您尝试在窗体的构造函数中设置单元格属性时,WinForms 中的 DataGridView 控件很挑剔。

请尝试改用 OnLoad 覆盖:

protected override void OnLoad(EventArgs e) {
  base.OnLoad(e);
  // your DataGridView code here...
}

像这样:

DataGridview1.Columns[index].DefaultCellStyle.BackColor = Color.Gray;

你可以这样尝试

                if (cells[0].Value!= null)
                if ((cells[0].Value as string ).Contains("==="))/*end of section*/
                {
                    if(c == Color.Gray)
                    {
                      c = Color.Transparent;                              
                    }
                    else
                    {                 
                       c = Color.Gray;
                    }
                }
int rowIndex = 0;
        int cellIndex = 0;
        Color c = Color.Gray;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            var cells = row.Cells;
            if (cells.Count > 0)
            {
                cellIndex = 0;
                foreach (DataGridViewCell cell in cells)
                {
                    dataGridView1.Rows[rowIndex].Cells[cellIndex].Style.BackColor = c;
                    cellIndex++;
                }
                if (cells[0].Value != null)
                    if ((cells[0].Value as string).Contains("==="))
                    {
                        c = ((c == Color.Gray) ? Color.Transparent : Color.Gray);
                    }
            }
            rowIndex++;
        }
        rowIndex = 0;