Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c# - WinForm DataGrid Change cell color

I have a DataGrid Object in a WinForm project

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

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"

my column 3 is a string and named 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"

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)

I have a lot of code and I try to put here the minimal.. tell me if you miss any information


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Customizing appearance of DataGrid

To customize a DataGrid control, you usually need to define a DataGridTableStyle and add a few DataGridColumnStyle to it.

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. Then you can customize the behavior by overriding properties and methods of the 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. 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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...