【问题标题】:how to change row color on RowsAdded event in c#如何在 C# 中更改 RowsAdded 事件的行颜色
【发布时间】:2012-05-28 12:22:29
【问题描述】:

我有一个 dataGridview,它每 1 分钟刷新一次,并在其中添加新行(dataGridView)。现在我想根据某些条件更改那些新添加的行的前景色。请告诉我如何实现它?

问候 祖海布

【问题讨论】:

标签: c# datagridview colors


【解决方案1】:

为此,您可以从 DataGridView 的 _RowLeave() 事件中播放... 同样对于 NewRow(),您可以检查单元格值是否为空白或 Not Exist(),然后它可以应用于 dataGridView 单元格样式.. 就像下面的方式....

Form_Load()
{
    DataGridViewCellStyle AStyle = new DataGridViewCellStyle();
    AStyle.BackColor = Color.BlueViolet;

    blah...blah...blah..
}

private void MyDataGrid1_RowLeave(Object Sender, DataGridViewCellEventArgs e)
{
    for (int I1 = 0; I1 < dataGrid1.Columns.Count - 1; I1++)
        {
            if (I1 == 3 || I1 == 5)
                {
                    dataGrid1.CurrentRow.Cells[I1].Style = AStyle;   
                }

    }

}

谢谢

【讨论】:

    【解决方案2】:

    您可以在 RowsAdded 事件中更改单元格字体。我是用 Visual Basic 做的,所以你可以把它翻译成 c#,这里是代码:

        Private Sub DatagridView_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DatagridView.RowsAdded
        With DirectCast(sender, DataGridView)
            If .Item(yourColumnIndex, e.RowIndex).Value Is "yourValue" Then
                .Rows.Item(e.RowIndex).DefaultCellStyle.ForeColor = Color.White
                .Rows.Item(e.RowIndex).DefaultCellStyle.BackColor = Color.DarkRed
                .Rows.Item(e.RowIndex).DefaultCellStyle.Font = New Font("Verdana", 8, FontStyle.Strikeout Or FontStyle.Bold)
            End If
        End With
    End Sub
    

    我希望这对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多