如何以编程方式为数据网格行设置背景颜色

时间:2010-12-07 17:32:42

标签: winforms c#-3.0

我在Windows窗体中有一个datagrid(不是gridview或datagridview)。它是在Microsoft Visual Studio 2003中创建的。我已经转换为2008.我应该根据条件更改数据网格的数据行。

我用Google搜索并找到了一些例子,例如

void myDataGrid_LoadingRow(object sender,DataGridRowEventArgs e)

但我没有任何“DataGridRowEventArgs”参数。

我也在

中找到了一个

http://www.syncfusion.com/faq/windowsforms/faq_c44c.aspx ,它们会改变某个特定单元格的颜色。

但是如何根据某些条件更改Windows窗体中Datagrid中整行的颜色。

提前致谢。

此致

SKR

1 个答案:

答案 0 :(得分:0)

使用此提示:

    private void dataGridView1_CellFormatting(object sender,         DataGridViewCellFormattingEventArgs e)
{
    foreach (DataGridViewRow Myrow in dataGridView1.Rows) 
    {            //Here 2 cell is target value and 1 cell is Volume
        if (Convert.ToInt32(Myrow .Cells[2].Value)<Convert.ToInt32(Myrow .Cells[1].Value))// Or your condition 
        {
            Myrow .DefaultCellStyle.BackColor = Color.Red; 
        }
        else
        {
            Myrow .DefaultCellStyle.BackColor = Color.Green; 
        }
    }
相关问题