如何使 DataGridViewLinkColumn 下划线并更改背景颜色 [英] How to make DataGridViewLinkColumn underline and change background color

查看:20
本文介绍了如何使 DataGridViewLinkColumn 下划线并更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridViewLinkColumn.如何将标题(行 = -1)设为下划线并更改其背景颜色

I have a DataGridViewLinkColumn.How can I make the header(row = -1) as underline and change it's background color

var WarningsColumn = new DataGridViewLinkColumn
            {

                Name = @"Warnings",
                HeaderText = @"Warnings",
                DataPropertyName = @"WarningsCount",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,               
                ReadOnly = true
            };

推荐答案

我认为您必须像这样将自定义代码添加到 CellPainting 事件处理程序:

I think you have to add custom code to a CellPainting event handler like this:

 Point spot;
 private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex == -1 && e.ColumnIndex > -1)
        {
            e.Handled = true;
            if (e.CellBounds.Contains(spot))//Mouse over cell
            {
                PaintCellBackground(e.Graphics, Color.Red, e.CellBounds);
            }
            else //Mouse leave cell
            {
                PaintCellBackground(e.Graphics, Color.Green, e.CellBounds);
            }
            StringFormat sf = new StringFormat(){Alignment=StringAlignment.Center, LineAlignment = StringAlignment.Center };
            Font f = new Font(e.CellStyle.Font, FontStyle.Underline);
            e.Graphics.DrawString(e.Value.ToString(), f, new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf);
        }
    } 
 private void PaintCellBackground(Graphics g, Color c, Rectangle rect)
    {
        Rectangle topHalf = new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height / 2);
        Rectangle bottomHalf = new Rectangle(rect.Left, topHalf.Bottom, rect.Width, topHalf.Height);
        g.FillRectangle(new SolidBrush(Color.FromArgb(150, c)), topHalf);
        g.FillRectangle(new SolidBrush(c), bottomHalf);
        ControlPaint.DrawBorder(g, rect, Color.Gray, 1, ButtonBorderStyle.Solid, 
                                         Color.Gray, 0, ButtonBorderStyle.Solid, 
                                         Color.Gray, 1, ButtonBorderStyle.Solid, 
                                         Color.Gray, 0, ButtonBorderStyle.Solid);
    }
    //Reset spot when mouse leave
    private void dataGridView_MouseLeave(object sender, EventArgs e)
    {
        spot = Point.Empty;
    }
    //Update spot when mouse move 
    private void dataGridView_MouseMove(object sender, MouseEventArgs e)
    {
        spot = e.Location;
    }

看起来不太好但可以帮助你入门,我认为默认背景更好.如果是这样,你只需要调用:e.PaintBackground(e.CellBounds, true);

It looks not good but it can help you get started, I think the default background is better. If so, you just need to call: e.PaintBackground(e.CellBounds, true);

自定义绘画应该应用于 DoubleBuffered 控件.所以我认为你应该像这样创建你自己的自定义 DataGridView(它只是多一点代码):

The custom painting should be applied on DoubleBuffered control. So I think you should create your own custom DataGridView like this (it's just a little more code):

public class CustomDataGridView : DataGridView {
    public CustomDataGridView(){
       DoubleBuffered = true;
    }
}

这篇关于如何使 DataGridViewLinkColumn 下划线并更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆