首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使DataGridViewLinkColumn加下划线并更改背景颜色

如何使DataGridViewLinkColumn加下划线并更改背景颜色
EN

Stack Overflow用户
提问于 2013-06-08 11:38:05
回答 2查看 2.7K关注 0票数 0

我可以将标题( DataGridViewLinkColumn.How = -1)设置为下划线并更改其背景颜色吗

代码语言:javascript
复制
var WarningsColumn = new DataGridViewLinkColumn
            {

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-08 11:48:30

我认为您必须向CellPainting事件处理程序添加自定义代码,如下所示:

代码语言:javascript
复制
 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);

更新

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

代码语言:javascript
复制
public class CustomDataGridView : DataGridView {
    public CustomDataGridView(){
       DoubleBuffered = true;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2013-06-08 11:55:19

试试这个:

代码语言:javascript
复制
dataGridView1.EnableHeadersVisualStyles = false;

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

来自DataGridView.ColumnHeadersDefaultCellStyle Property上的MSDN参考

如果启用了视觉样式并且EnableHeadersVisualStyles设置为true,则使用当前主题绘制除TopLeftHeaderCell之外的所有标题单元格,并忽略ColumnHeadersDefaultCellStyle值。

因此,您可以将其设置为False,然后覆盖默认设置,然后您将得到如下所示的结果(快速测试以确保其正常工作)

编辑:

要将样式应用于单个列,请改用以下代码(您需要将此代码放在设置DataGridView__的DataSource的代码后面):

代码语言:javascript
复制
dataGridView1.Columns["your_column_name"].HeaderCell.Style
    = new DataGridViewCellStyle { BackColor = Color.Yellow, Font = new Font(dataGridView1.Font, FontStyle.Underline) };

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16995775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档