我有一个包含数据的 GridView 表..出于某种原因我想填充每个单元格的背景颜色..但这里有一个问题..我只需要填充单元格大小的 1/10颜色。是否有可能做到这一点?如果是的话——怎么办? 我正在使用 winforms 和 c#。
非常感谢。
最佳答案
也许尝试这个解决方案(已测试)
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;
}
}
关于c# - 是否可以在 C# 中仅填充 GridView 的半个单元格背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19117017/