【问题标题】:how to set a gradient color for each row in datagridview after data binding数据绑定后如何为datagridview中的每一行设置渐变颜色
【发布时间】:2011-06-20 04:53:38
【问题描述】:

我想在数据绑定后为datagridview中的每一行设置一个渐变颜色(我的意思是在DataBoundCompleted事件中)

我看到了这篇文章,但它们都是用于选择一行的。我想为每一行设置渐变。

DataGridView, add unique gradient to each Row

How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control

谢谢

【问题讨论】:

    标签: c# winforms c#-4.0 datagridview


    【解决方案1】:

    您链接到的第二个示例正是您需要的。网格中的每一行都会触发 DataGridView.RowPrePaint 事件。

    此事件的文档页面上的示例仅自定义呈现选定的行,因为它包含以下检查。

    // Determine whether the cell should be painted
    // with the custom selection background.
    if ((e.State & DataGridViewElementStates.Selected) ==
                DataGridViewElementStates.Selected)
    

    删除此检查,您将看到每一行都有自定义背景。

    【讨论】:

    • 我删除了该代码,但渐变颜色不适用于行。我不知道为什么。似乎默认样式会覆盖此样式
    【解决方案2】:

    我不知道这是否对在谷歌搜索后登陆这里的人有帮助,我基于我在另一个论坛中找到的一些 vb 代码:

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace CPS
    {
        class gradientGrid : DataGridView
        {
    
            protected override void PaintBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle gridBounds)
            {
                base.PaintBackground(graphics, clipBounds, gridBounds);
    
                System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(clipBounds, Color.CadetBlue, Color.AntiqueWhite, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
                graphics.FillRectangle(b, clipBounds);
            }
        }
    }
    

    一旦你将它构建为一个单独的类,它就会显示在你的 (VS2010) 工具箱中,然后你将它放到你的表单中......

    此代码适用于整个数据网格视图,因此要对每一行执行此操作,您可以使用 RowPrePaint 事件...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2020-04-01
      • 2017-07-30
      • 2017-09-17
      相关资源
      最近更新 更多