DatagridView颜色控件不适用于新表单



我在主窗体上有一个数据网格。单击单元格时,我正在创建一个新窗体,在该窗体上显示一个新的数据网格。在这个新网格上,我想更改某个单元格的背景颜色。

我尝试过使用:

  UnitData_DataGridView.Rows[index].Cells[5].Style.BackColor = Color.Orange;

但它不起作用。

附件是代码:

     private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
    {
               Form frm = new Form();                   
                DataGridView UnitData_DataGridView = new System.Windows.Forms.DataGridView();

                Controls.Add(UnitData_DataGridView);
                frm.Controls.Add(UnitData_DataGridView);
                DataTable table = new DataTable();
                List<string[]> output = new List<string[]>();
                for (int i = 0; i < str.Count(); i++)
                {
                    table.Columns.Add(str[i]);
                }
                for (int i = 0; i < count; i++)
                {
                    DataRow row = table.NewRow();
                    for (int j = 0; j < strline.Count(); j++)
                    {
                        row[j] = strline[j];
                    }
                    table.Rows.Add(row);
                }
                UnitData_DataGridView.DataSource = table;
                UnitData_DataGridView.Size = new Size(1000, 500);
                UnitData_DataGridView.Enabled = true;
                UnitData_DataGridView.ReadOnly = true;
                UnitData_DataGridView.ScrollBars = ScrollBars.Both;
                foreach (DataGridViewRow row in UnitData_DataGridView.Rows)
                {

                     if (row.Cells[8].Value.ToString() == "B")
                    {
                        UnitData_DataGridView.Rows[index].Cells[8].Style.BackColor = Color.Brown;
                    }
                     if (row.Cells[3].Value.ToString().StartsWith("ERROR"))
                    UnitData_DataGridView.Rows[index].Cells[3].Style.BackColor = Color.Yellow;
                }

                frm.Location = new Point(0, 0);
                frm.Size = Screen.PrimaryScreen.WorkingArea.Size;
                frm.AutoScroll = true;
                frm.Show();

    }

我发现您的代码有几个问题..:

第一行被第二行抵消:

Controls.Add(UnitData_DataGridView);  // Remove!!
frm.Controls.Add(UnitData_DataGridView);

第一个将其添加到当前表单。第二个将其移动Form frm

只有一个DataGridView,并将其添加到另一个Controls集合,将其从以前的集合中删除。。

你可能只想要第二行。

  • 另一个错误来源是使用CellContentClick..:它只在实际有内容并且你点击它时才会触发。如果单元格为空或者你不需要实际点击内容,请使用CellClick事件

根据您的需要,您可能想要或不想要设置

UnitData_DataGridView.AllowUserToAddRows = false;
  • 您通过变量index引用一行,但在您显示的代码中,这并没有设置也许您想将其设置为index = row.Index?但实际上你已经有了DataGridViewRow row,并且可以使用它:row.Cells[8].Style.BackColor = Color.Brown;。但是,如果您只想引用第0行,为什么不给它一个好的、有用的名称呢…:

    int rowOne=0;

  • 无论如何,问题的真正原因有点棘手:在is开始绘制之前,您正试图改变DataGridView的外观。不确定为什么这会导致问题,但确实如此。

为了实现这一点,我找到了比稍微改变事件顺序更好的方法:在绘制DGV之前显示新形式

将初始大小保持为零没有问题,因此不会出现视觉闪烁。:

...
UnitData_DataGridView.ScrollBars = ScrollBars.Both;
frm.Size = new Size(0,0);
frm.Show();
foreach (DataGridViewRow row in UnitData_DataGridView.Rows)
{
  ...
  ...
  ...
}
frm.Location = new Point(0, 0);
frm.Size =  Screen.PrimaryScreen.WorkingArea.Size;
frm.AutoScroll = true;

是的,这是一个解决方法,但我没有找到更直接的方法;Refreshing或各种Invalidate呼叫都无济于事。。

也许将绘画代码提取到函数中是个好主意。。

若要格式化DataGridView单元格,请使用CellFormatting事件。

这种做法将允许您对数据网格视图进行排序或筛选,而无需重新初始化所有单元格格式。

 private void  UnitData_DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex==8 && e.Value.ToString()=="B"               ) e.CellStyle.BackColor = Color.Brown ;
    if (e.ColumnIndex==3 && e.Value.ToString().StartsWith("ERROR")) e.CellStyle.BackColor = Color.Yellow;
}

最新更新