溫馨提示×

datagridview行顏色自定義如何做

小樊
315
2024-06-27 13:08:22
欄目: 編程語言

要自定義DataGridView的行顏色,可以使用DataGridView的RowsDefaultCellStyle屬性來設置默認行樣式,也可以在DataGridView的RowPrePaint事件中自定義每一行的顏色。

以下是兩種方法的示例代碼:

  1. 使用RowsDefaultCellStyle屬性設置默認行樣式:
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
dataGridView1.RowsDefaultCellStyle.ForeColor = Color.Black;
  1. 在RowPrePaint事件中自定義每一行的顏色:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    if (e.RowIndex % 2 == 0)
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGray;
    }
    else
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
    }
}

以上代碼中,當行號為偶數時,設置行的背景顏色為淺灰色;當行號為奇數時,設置行的背景顏色為白色。您可以根據需求自定義每一行的顏色。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女