如何为DataGridView中存在的复选框设置颜色?

发布于 2025-01-28 12:59:24 字数 699 浏览 3 评论 0原文

我在Windows表单中有一个DataGridView。看起来像这样。

在此中,有一个复选框列,两个文本框列。

我的要求是需要将Readonly True设置为True,并为该国的复选框设置灰色,如下所示。

不喜欢下面的

”在此处输入图像说明“

我需要为复选框设置颜色,而不是为datagridview单元格。

有人对此有主意吗?

I have a datagridview in windows form. It looks like this.

enter image description here

In this, have one Checkbox column, two textbox column.

My requirement is need to set readonly true and set grey color for the checkbox for the country has Germany as like below.

enter image description here

Not like below

enter image description here

I need to set color for checkbox only not for datagridview cell.

Is anyone have idea for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2025-02-04 12:59:24

给定答案在这里 和稍微更改

private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    var value = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
    DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[0];

    if (value == "Germany" && cell.ReadOnly != true)
    {
        DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
        chkCell.Value = false;
        chkCell.FlatStyle = FlatStyle.Flat;
        chkCell.Style.ForeColor = Color.DarkGray;
        cell.ReadOnly = true;
    }
}

结果

”

Given the answer here and slightly change it

private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    var value = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
    DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[0];

    if (value == "Germany" && cell.ReadOnly != true)
    {
        DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
        chkCell.Value = false;
        chkCell.FlatStyle = FlatStyle.Flat;
        chkCell.Style.ForeColor = Color.DarkGray;
        cell.ReadOnly = true;
    }
}

Result

enter image description here

若能看破又如何 2025-02-04 12:59:24

您可以通过设置Checkbox的属性来实现:“启用”为false。
这样,无法单击复选框,其颜色会自动更改为灰色。
示例

You can achieve that by setting checkbox's property: 'Enabled' as false.
by that way, the checkbox cannot be clicked and its color changes automatically to grey.
Example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文