如何更改网格载荷上的列颜色? [英] How do i change column color on grid load?

查看:94
本文介绍了如何更改网格载荷上的列颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是否说明,如何在网格启动时设置列颜色?我想知道如果列有值,是否有可能将颜色从较浅的颜色更改为较暗的颜色。





以此为例:



A列:



如果该列上的单元格没有值,则为浅绿色。

深绿色如果该列上的单元格有值。



B列:



如果该列上的单元格没有值,则为红色。

如果该列上的单元格没有值,则为暗红色。



我尝试了什么:



尝试使用方法Grid1.Cells [i] .Toint16(string val)但不起作用按照我的预期。

Has the title says, how can I set a column colour on grid start up? And I want to know if is any possibility to change the colour from a lighter colour to a darker colour if the column has values.


Take this for example:

Column A:

Light Green if the cell on that column has no value.
Dark Green if the cell on that column has a value.

Column B:

Light Red if the cell on that column has no value.
Dark Red if the cell on that column has no value.

What I have tried:

Tried to use the method Grid1.Cells[i].Toint16(string val) but didn't work out as I expected.

推荐答案

您可以使用 DataBindingComplete 事件,该事件将在网格被触发后触发填充了价值观。你必须逐步完成每一行,例如

You can use the DataBindingComplete event which will be fired after the grid has been populated with values. You'll have to step through each of the rows e.g.
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if( string.IsNullOrEmpty(row.Cells[0].Value.ToString()))
            row.Cells[0].Style = new DataGridViewCellStyle {BackColor = Color.LightGreen};
        else
            row.Cells[0].Style = new DataGridViewCellStyle {BackColor = Color.DarkGreen};

        if( string.IsNullOrEmpty(row.Cells[1].Value.ToString()))
            row.Cells[1].Style = new DataGridViewCellStyle {BackColor = Color.Red};
        else
            row.Cells[1].Style = new DataGridViewCellStyle {BackColor = Color.DarkRed};
    }
}



我本可以使用?运算符,但我不喜欢很长的代码行


I could have used the ? operator but I don't like really long lines of code

row.Cells[0].Style = string.IsNullOrEmpty(row.Cells[0].Value.ToString()) ? new DataGridViewCellStyle {BackColor = Color.LightGreen} : new DataGridViewCellStyle {BackColor = Color.DarkGreen};


这篇关于如何更改网格载荷上的列颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆