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

标签 c# winforms

我有一个包含数据的 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/

相关文章:

c# - 我如何告诉设计者我的自定义 winforms 控件具有固定高度?

c# - Xamarin Forms Tap 手势识别器 Ios 不起作用

c# - 无法设置类型的属性,因为集合已设置为 EntityCollection

c# - 错误CS0201-无法理解我的代码出了什么问题

winforms - 在 XNA 中使用 system.windows.forms

c# - 我需要在 Visual C# 或更好的建议中创建一个输入对话框?

winforms - 在 winforms 应用程序中调整主窗体大小时如何调整面板大小?

c# - 读取包含希腊字符的 CSV 文件

c# - Ninject 3 如何影响对象的生命周期?

c# - 在 C# 和 WinForms 中将筛选框添加到 ListView 的列标题