Winforms datatable(非datagridview)根据字段更改行的颜色

Winforms datatable(非datagridview)根据字段更改行的颜色,winforms,Winforms,如何根据Windows Forms C#中的字段更改数据表行的颜色(例如:如果某些字段为空,我想更改行的颜色) Datatable有一个属性?您需要挂接datagridview的CellFormatting事件,如MSDN上所示,如下所示: private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e) { /

如何根据Windows Forms C#中的字段更改数据表行的颜色(例如:如果某些字段为空,我想更改行的颜色)


Datatable有一个属性?

您需要挂接datagridview的CellFormatting事件,如MSDN上所示,如下所示:

private void dataGridView1_CellFormatting(object sender, 
    System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    // check against your column name here
    if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
    {
        // we are now in the correct column
        String stringValue = e.Value as string;
        DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];

        switch (stringValue)
        {
            case "high":
                cell.Style.BackColor = Color.Red;
                break;
            case "medium":
               ...
                break;
    }

    }
}

甚至有datatable UI控件这样的东西吗?啊,对不起,datatable是dataGridView1的源,谢谢dataGridView1。行[Rowindex]。DefaultCellStyle.BackColor=Color.LightBlue;