【问题标题】:DatagridView color controls not working on new formDatagridView 颜色控件不适用于新表单
【发布时间】:2015-09-19 02:40:18
【问题描述】:

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

我尝试过使用:

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


    }

【问题讨论】:

    标签: c# winforms datagridview datagrid desktop-application


    【解决方案1】:

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

    第一行被第二行抵消了:

    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;

    • 无论如何,问题的真正原因有点棘手:您正在尝试更改 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 电话没有帮助..

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

    【讨论】:

    • 我想更改我新创建的表单上某些数据网格单元格的背景颜色。但同样的实施是行不通的。它加载新表单,但使用默认颜色。即使我删除 Controls.Add(UnitData_DataGridView); , 特定单元格的背景颜色不会改变。
    • 你的单元格真的有一些内容吗?如果不是,它不能工作使用 CellClick.. 你有没有使用调试器查看你设置颜色的行是否真的被击中??
    • 我已经检查了空单元格,并且在调试器中,它也会执行设置颜色的行。
    【解决方案2】:

    要格式化 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;
    }
    

    【讨论】:

    • 感谢您的建议。我试过这个,单元格的背景颜色现在正在改变。但现在我得到“对象引用未设置为对象的实例”。
    • 某些单元格值可能为空(没有测试,因为您没有在代码中添加任何值)。然后在两个测试中的“&& e.value.Tostring...”之前添加“&& e.value!=null”
    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多