简体   繁体   English

WinForm DataGrid 更改单元格颜色

[英]WinForm DataGrid Change cell color

I have a DataGrid Object in a WinForm project我在 WinForm 项目中有一个 DataGrid Object

Friend WithEvents StatusTable As System.Windows.Forms.DataGrid

I also have a DataSet Private StatusDS As Data.DataSet with data that load in StatusTable like我还有一个 DataSet Private StatusDS As Data.DataSet ,其中的数据加载到 StatusTable 中,例如

StatusTable.DataSource = StatusDS.Tables("Channels")

Everything just works fine, But now I have to change the full row background color when column 3 and row x is "12"一切正常,但现在我必须在第 3 列和第 x 行为“12”时更改整行背景颜色

my column 3 is a string and named Act我的第 3 列是一个字符串,名为 Act

my column Act updates like我的专栏更新像

 Dim ts As New DataGridTableStyle

 Dim Act As New DataGridTextBoxColumn

 Act.MappingName = "Act"
 Act.Alignment = HorizontalAlignment.Center
 Act.HeaderText = "Action"
 
 ts.GridColumnStyles.Add(Act)

 StatusTable.TableStyles.Add(ts)

I can't find any way to change the row background color if the column act and row X is "12"如果列 act 和行 X 为“12”,我找不到任何方法来更改行背景颜色

for example, if I have this table例如,如果我有这张桌子

Act
"1"
"12"
"1"
"4"
"12"

I have to change the color of rows number 2 and 5 (the first start in 1)我必须更改第 2 行和第 5 行的颜色(第一个从 1 开始)

I have a lot of code and I try to put here the minimal.. tell me if you miss any information我有很多代码,我试着把最小的..告诉我你是否错过了任何信息

Note: The question is about Windows Forms .NET DataGrid which is deprecated and no longer available in .NET CORE and .NET 5. It's strongly recommended to upgrade your solution to use DataGridView ; Note: The question is about Windows Forms .NET DataGrid which is deprecated and no longer available in .NET CORE and .NET 5. It's strongly recommended to upgrade your solution to use DataGridView ;

Anyways, if you are stuck with DataGrid and cannot easily update to DataGridView, the answer is for you.无论如何,如果您被 DataGrid 卡住并且无法轻松更新到 DataGridView,那么答案就是给您的。

Customizing appearance of DataGrid自定义 DataGrid 的外观

To customize a DataGrid control, you usually need to define a DataGridTableStyle and add a few DataGridColumnStyle to it.要自定义一个DataGrid控件,通常需要定义一个DataGridTableStyle并为其添加一些DataGridColumnStyle

To have full control on appearance of the rows and columns, you usually need to create custom column styles by driving from one of the existing built-in column styles like DataGridTextBoxColumn or DataGridBoolColumn or the base DataGridColumnStyle .要完全控制行和列的外观,您通常需要从现有的内置列 styles 之一(如DataGridTextBoxColumnDataGridBoolColumn或基本DataGridColumnStyle )驱动来创建自定义列 styles。 Then you can customize the behavior by overriding properties and methods of the class.然后,您可以通过覆盖 class 的属性和方法来自定义行为。

You will find this article very useful:你会发现这篇文章非常有用:

Example - Change row background color based on cell value of a specific column示例 - 根据特定列的单元格值更改行背景颜色

Here I create a new column style by deriving from the built-in DataGridTextBoxColumn and overriding its Paint method.在这里,我通过从内置DataGridTextBoxColumn派生并覆盖其Paint方法来创建新的列样式。 In the method, I check if the value of the first column is odd number:在该方法中,我检查第一列的值是否为奇数:

public class MyDataGridTextBoxColumn : DataGridTextBoxColumn
{
    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source,
        int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
        var idCellValue = ((DataRowView)source.List[rowNum])["Id"];
        var brush = backBrush;
        if (idCellValue != DBNull.Value && ((int)idCellValue) % 2 == 1)
            brush = Brushes.Yellow;
        base.Paint(g, bounds, source, rowNum, brush, foreBrush, alignToRight);
    }
}

Then to use it:然后使用它:

private void Form1_Load(object sender, EventArgs e)
{
    var dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Rows.Add(1, "A");
    dt.Rows.Add(2, "B");
    dt.Rows.Add(3, "C");

    var dg = new DataGrid();
    dg.Dock = DockStyle.Fill;
    var ts = new DataGridTableStyle();
    ts.GridColumnStyles.Add(new MyDataGridTextBoxColumn()
    { MappingName = "Id", HeaderText = "Id" });
    ts.GridColumnStyles.Add(new MyDataGridTextBoxColumn()
    { MappingName = "Name", HeaderText = "Name" });
    dg.TableStyles.Add(ts);

    this.Controls.Add(dg);
    dg.DataSource = dt;
}

And you will see the result:你会看到结果:

在此处输入图像描述

Note: It definitely makes more sense to not put any business logic inside the custom Column, instead you can raise a Formatting event, and put any logic inside the event handler and pass formatting data back to the column, using event arguments.注意:不将任何业务逻辑放在自定义列中肯定更有意义,相反,您可以引发 Formatting 事件,并将任何逻辑放在事件处理程序中并将格式化数据传递回列,使用事件 arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM