在DataGridView上更改行标题的背景颜色,而不会失去默认样式

7

我只想改变某些行标题的背景颜色,而不失去DataGridView默认的酷炫Windows样式:

Grid.EnableHeadersVisualStyles = false;
for(int i=0; i<Grid.Rows.Count; i++)
{
    if ( /*I want to change this row */)
    {
        DataGridViewCellStyle rowStyle = Grid.RowHeadersDefaultCellStyle;
        rowStyle.BackColor = Color.Wheat;
        Grid.Rows[i].HeaderCell.Style = rowStyle;
    }
}

一旦我这样做,列的MouseOver蓝色效果就会消失,列上的排序箭头会变灰。 我尝试将列标题设置为defaultColHeaderStyle,但无效。行标题更改为所需颜色,但是列标题失去了其流畅的Windows样式。有什么帮助吗?
Grid.EnableHeadersVisualStyles = true Grid.EnableHeadersVisualStyles = false
1个回答

9

在构建 DataGridView 时,默认情况下应该已经定义了行标题的默认样式。因此,我会使用以下代码:

if ( /*I want to change this row */)
{
    DataGridViewCellStyle rowStyle; // = Grid.RowHeadersDefaultCellStyle;
    rowStyle = Grid.Rows[i].HeaderCell.Style;
    rowStyle.BackColor = Color.Wheat;
    Grid.Rows[i].HeaderCell.Style = rowStyle;
}

这样,您可以使用预定义的样式填充rowStyle,然后仅更改您想更改的部分。看看这是否解决了您的问题。
//编辑 由于您希望保留默认的Windows DataGridView的其他样式,因此您还需要设置更多样式的其他参数。请参见此帖子。 或者尝试这个。在初始化时:
        dataGridView1.CellPainting += 
  new DataGridViewCellPaintingEventHandler (dataGridView_CellPainting);

然后,使用类似以下方式创建处理程序函数:
    void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        DataGridView dv = sender as DataGridView;
        DataGridViewCellStyle rowStyle;// = dv.RowHeadersDefaultCellStyle;

        if (e.ColumnIndex == -1)
        {
            e.PaintBackground(e.CellBounds, true);
            e.Handled = true;
            if (/*I want to change this row */)
            {
                rowStyle = dv.Rows[e.RowIndex].HeaderCell.Style;
                rowStyle.BackColor = Color.Wheat;
                dv.Rows[e.RowIndex].HeaderCell.Style = rowStyle;
                using (Brush gridBrush = new SolidBrush(Color.Wheat))
                {
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // Clear cell 
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                            //Bottom line drawing
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);

                            // here you force paint of content
                            e.PaintContent(e.ClipBounds);
                            e.Handled = true;
                        }
                    }
                }
            }
        }
    }

这段代码基于这篇文章。你只需要为鼠标悬停和选中状态创建更多的绘制条件。但是这应该对你有用。

记得移除:Grid.EnableHeadersVisualStyles = false; 或者强制使用:Grid.EnableHeadersVisualStyles = true;


问题出在将Grid.EnableHeadersVisualStyles设置为false上。行标题会变色,但是列标题则会变得平淡无奇,不像启用Grid.EnableHeadersVisualStyles为true时那样好看。我尝试了您提供的列标题的解决方案(获取并重置样式),但它并没有起作用...我会发布一些图片来说明我的问题。 - Tizz
我现在更好地理解你所说的内容了,有了图片。嗯,这可能不太可能。 - CaptainBli
您可以使用 paint 处理程序修改单个行。 - CaptainBli
请确保不要设置 Grid.EnableHeadersVisualStyles = false;,它必须为 true 才能保持实际的默认样式。 - CaptainBli
很抱歉你必须手动绘制所有内容,但没关系。谢谢!我需要进行一些微调,但这绝对是一个好的方向! - Tizz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接