How to make DataGridViewLinkColumn underline and c

2019-09-18 18:12发布

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
            };

2条回答
霸刀☆藐视天下
2楼-- · 2019-09-18 18:45

Try this:

dataGridView1.EnableHeadersVisualStyles = false;

dataGridView1.ColumnHeadersDefaultCellStyle
    = new DataGridViewCellStyle {BackColor = Color.Yellow, Font = new Font(dataGridView1.Font, FontStyle.Underline)};

From the MSDN reference on DataGridView.ColumnHeadersDefaultCellStyle Property:

If visual styles are enabled and EnableHeadersVisualStyles is set to true, all header cells except the TopLeftHeaderCell are painted using the current theme and the ColumnHeadersDefaultCellStyle values are ignored.

So you can set it to False and then override the defaults, and then you'll end up with something like this (quick and dirty test to make sure it works):

enter image description here

Edit:

To apply a style to a single column, use this instead (you'll want to put this after the code where you set the DataSource of the DataGridView):

dataGridView1.Columns["your_column_name"].HeaderCell.Style
    = new DataGridViewCellStyle { BackColor = Color.Yellow, Font = new Font(dataGridView1.Font, FontStyle.Underline) };

enter image description here

查看更多
Juvenile、少年°
3楼-- · 2019-09-18 18:50

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;
    }

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);

UPDATE

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;
    }
}
查看更多
登录 后发表回答