c# - Winform C# Datagridview paint header

标签 c# winforms datagridview paint

我想使用 C# 在 Windows 窗体中自定义我的数据 GridView 。我想要做的是用我制作的渐变绘制数据网格标题:

public void Colorear_Barra_abajo(object sender, PaintEventArgs e)
    {
        Rectangle r = new Rectangle(0,0, panel_Borde_abajo.Width, panel_Borde_abajo.Height);

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 62, 62, 62);
            Color c3 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c3, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.5, 1 };
            cb.Colors = new[] { c1, c2 , c3 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

问题是 datagridviews 没有绘画事件,所以我不能使用它。有没有办法用渐变绘制标题?或者唯一的方法是选择一种背景色?

谢谢..

最佳答案

如果您指的是 Column Header,是的。在 CellPainting 事件

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex == -1)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 62, 62, 62);
            Color c3 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c3, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.5, 1 };
            cb.Colors = new[] { c1, c2, c3 };
            br.InterpolationColors = cb;


            e.Graphics.FillRectangle(br, e.CellBounds);
            e.PaintContent(e.ClipBounds);
            e.Handled = true;
        }
    }

关于c# - Winform C# Datagridview paint header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012475/

相关文章:

c# - 参数索引超出范围

c# - 使用.NET是否可以将自定义属性分配给诸如FileSystemWatcher的内置对象?

c# - 如何在 DbContext 中并行运行 Xunit 而不会发生冲突 - 主键?

winforms - 自定义控件和 Visual Studio 2008 SP1

c# - 管理 .NET WinForm 应用程序中的线程

c# - 删除 gridview 选择器列

vb.net - 如何使用自定义控件来编辑 DataGridView 中单元格的内容

c# - 发布方法并使用某些值重定向而不使用 Form C#

c# - 您如何让只有授权用户才能退出应用程序或关闭特定表单?

c# - 通过在文本框中输入进行过滤