C# 如何在数据绑定后为datagridview中的每一行设置渐变颜色
我想在数据绑定后为datagridview中的每一行设置渐变颜色(我是指在DataBoundCompleted事件中) 我看到了这篇文章,但它们都是为选择一行。我想为每一行设置梯度C# 如何在数据绑定后为datagridview中的每一行设置渐变颜色,c#,winforms,c#-4.0,datagridview,C#,Winforms,C# 4.0,Datagridview,我想在数据绑定后为datagridview中的每一行设置渐变颜色(我是指在DataBoundCompleted事件中) 我看到了这篇文章,但它们都是为选择一行。我想为每一行设置梯度 谢谢您链接到的第二个示例正是您需要的示例。将为网格中的每一行触发该事件 此事件文档页面上的示例仅自定义呈现所选行,因为它包含以下检查 // Determine whether the cell should be painted // with the custom selection background. i
谢谢您链接到的第二个示例正是您需要的示例。将为网格中的每一行触发该事件 此事件文档页面上的示例仅自定义呈现所选行,因为它包含以下检查
// Determine whether the cell should be painted
// with the custom selection background.
if ((e.State & DataGridViewElementStates.Selected) ==
DataGridViewElementStates.Selected)
删除此复选框,您将看到每一行都有自定义背景。我不知道这是否有助于谷歌搜索后登陆此处的用户,我基于在另一个论坛中找到的一些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)工具箱中,然后你把它放到你的表单上
此代码适用于整个datagrid视图,因此要对每一行执行此操作,您可以使用RowPrePaint事件…我删除了该代码,但渐变色不适用于行。我不知道为什么。默认样式似乎覆盖了此样式