簡體   English   中英

更改DataGridView的行背景顏色

[英]Changing the row background color of DataGridView

我搜索了很多,並找到了一些方法來改變數據網格視圖中的行背景顏色,但它根本不起作用,你能幫我找到我的錯誤嗎?

dataGridViewResult.DataSource = morgan.GetResult().Tables[5];
        dataGridViewResult.Rows[0].Cells[1].Style.ForeColor = System.Drawing.Color.Beige;
        dataGridViewResult.Rows[0].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[7].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[40].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[11].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[19].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[28].DefaultCellStyle.ForeColor = Color.Blue;
        dataGridViewResult.Rows[35].DefaultCellStyle.ForeColor = Color.Blue;

        dataGridViewResult.Rows[35].DefaultCellStyle.ForeColor = Color.White;

此代碼可能有效:

private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
    // check the cell value under your specific column and then you can toggle your colors
    row.DefaultCellStyle.BackColor = Color.Green;
}

更多信息: DataGridView行的背景顏色沒有變化

m.lansers答案是對的。 您是否將他使用的方法綁定到DataGridViews CellFormatting事件? 如:

dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(grid1_CellFormatting);

另外,另一個較短的代碼就是使用:

e.CellStyle.BackColor = Color.Blue;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM