【问题标题】:painting DataGridView Row绘制 DataGridView 行
【发布时间】:2010-09-20 17:48:32
【问题描述】:

我正在尝试将 DataGridView 行的背景颜色设置为红色。我尝试了以下行:

dgvActiveCalls.Rows[1].DefaultCellStyle.BackColor = Color.Red;

但它不起作用。然后只是为了看看更新是否有问题,我尝试绘制列而不是行:

dgvActiveCalls.Columns[1].DefaultCellStyle.BackColor = Color.Red;

而且效果很好。如果有人能展示绘制 DataGridView 行的方法,我将非常感激。

谢谢!

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    我在我的机器上进行了测试,它运行良好。你确定那条线没有突出显示吗?如果它突出显示,您将看到突出显示的颜色,而不是红色。

    【讨论】:

      【解决方案2】:

      确保默认样式没有被其他样式覆盖。 2.0 中 DGV 的有趣怪癖:似乎继承链几乎与您的预期相反。如果您正在动态添加列,您的 DefaultCellStyle 可以被忽略并被 RowsDefaultCellStyle 覆盖。排序也可以覆盖您设置的样式。

      您可能想检查设置这些样式的顺序,并在 Google 上查找有关样式继承的一些文章。

      附:有趣的是,我用谷歌搜索了一个要提供的链接,并在这个博客上发现了一个几乎相同的解释:

      http://yakkowarner.blogspot.com/2008/06/datagridview-style-inheritance.html

      【讨论】:

        【解决方案3】:

        我建议您将该代码放在 DataGridView 的 RowPrePaint 事件中。例如:

        private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            /*Here you put your validation in order to paint only the row that you want*/
            if (e.RowIndex == 1)
            {
                 DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex];
                 row.DefaultCellStyle.BackColor = Color.Red;
            }                
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-17
          • 1970-01-01
          • 2012-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-06
          • 1970-01-01
          相关资源
          最近更新 更多