【问题标题】:Changing the row background color of DataGridView更改 DataGridView 的行背景颜色
【发布时间】:2015-02-14 12:41:26
【问题描述】:

我搜索了很多,找到了一些方法来更改数据网格视图中的行背景颜色,但它根本不起作用,你能帮我找出我的错误吗?

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;

【问题讨论】:

  • 您发布的代码更改了ForeColor,而不是BackColor。这是你想要的吗?粘贴错误?旁注:您将第 35 行更改了两次,从而覆盖了第一次更改。
  • @OhBeWise 实际上我已经测试了大多数单元格样式并且它们根本不起作用。它没有显示任何错误它只是没有改变颜色。关于第 35 行是的,这是我的错误但它不会改变任何东西

标签: c# datagridview


【解决方案1】:

此代码可能有效:

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 row's background color is not changing

【讨论】:

  • @MajidHojati 我检查了它,它的工作。什么是错误或什么错误
  • 这不会为在行中找到的每个单元格设置行的颜色吗?这意味着您重复设置相同的值。
【解决方案2】:

m.lansers 的答案是正确的。你有没有将他使用的方法绑定到 DataGridViews CellFormatting 事件上?如:

dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(grid1_CellFormatting);

另外,另一种较短的代码就是使用:

e.CellStyle.BackColor = Color.Blue;

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2011-03-11
    • 2016-05-27
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多