是否可以在 C# 中仅填充半个单元格背景颜色 GridView

本文关键字:半个 单元格 背景 GridView 颜色 填充 是否 | 更新日期: 2023-09-27 18:33:23

我是一个带有数据的 GridView 表......出于某种原因,我想填充每个单元格的背景颜色。但这里有一个问题..我只需要用颜色填充单元格大小的 1/10。可以这样做吗?如果是这样 - 如何?我正在将 winforms 与 c# 一起使用。

多谢。

是否可以在 C# 中仅填充半个单元格背景颜色 GridView

也许试试这个解决方案(经过测试)

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {   
        if (e.ColumnIndex >= 0 && e.ColumnIndex < 1 && e.RowIndex >= 0)
        {
            //Watch out for the Index of Rows (here 0 for testing)
            string text = this.dataGridView1.Rows[0].Cells[e.ColumnIndex].Value.ToString();
            // Clear cell 
            e.Graphics.FillRectangle(new SolidBrush(Color.Green), new Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width / 10 , e.CellBounds.Height));
            e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), new Point(e.CellBounds.Left, e.CellBounds.Top + 2));
            e.Graphics.DrawRectangle(new Pen(Color.Silver), new Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 1, e.CellBounds.Height - 1));
            e.Handled = true;
        }
    }
如果我

没记错的话,你可以使用CellPainting事件,但你必须自己画单元格。希望对您有所帮助。

看看这个。

如何只绘制数据网格视图的单元格背景而不是其内容?

在步骤中:

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

为矩形定义自己的边界,而不是使用单元格的边界。