2

我是一个包含数据的 GridView 表。出于某种原因,我想填充每个单元格的背景颜色。但这里有一个问题。我只需要用颜色填充单元格大小的 1/10。是否有可能做到这一点?如果确实如此 - 如何?我正在使用带有 c# 的 winforms。

非常感谢。

4

3 回答 3

2

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

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

    }
于 2013-10-01T13:40:58.617 回答
0

如果我没记错的话,您可以使用CellPainting事件,但实际上您必须自己绘制单元格。希望它可以帮助你。

于 2013-10-01T13:28:30.523 回答
0

看一下这个。

如何仅绘制 DataGridView 的单元格背景而不是其内容?

在步骤中:

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

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

于 2013-10-01T13:32:17.500 回答