DataGridView DataGridViewCheckBoxColumn 实现禁用效果,因为默认的不带禁用效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// <summary>
/// DataGridViewDisableCheckBoxColumn
/// 自定义disablecheckbox列 实现禁用效果
/// </summary>
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
    public DataGridViewDisableCheckBoxColumn()
    {
        this.CellTemplate = new DataGridViewDisableCheckBoxCell();
    }
}
 
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
{
    private bool enabledValue;
 
    public bool Enabled
    {
        get
        {
            return enabledValue;
        }
        set
        {
            enabledValue = value;
        }
    }
 
    public override object Clone()
    {
        DataGridViewDisableCheckBoxCell cell =
            (DataGridViewDisableCheckBoxCell)base.Clone();
        cell.Enabled = this.Enabled;
        return cell;
    }
 
    public DataGridViewDisableCheckBoxCell()
    {
        this.enabledValue = true;
    }
 
    protected override void Paint(Graphics graphics,
        Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates elementState, object value,
        object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        if (!this.enabledValue)
        {
            if ((paintParts & DataGridViewPaintParts.Background) ==
                DataGridViewPaintParts.Background)
            {
                SolidBrush cellBackground =
                    new SolidBrush(cellStyle.BackColor);
                graphics.FillRectangle(cellBackground, cellBounds);
                cellBackground.Dispose();
            }
 
            if ((paintParts & DataGridViewPaintParts.Border) ==
                DataGridViewPaintParts.Border)
            {
                PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
                    advancedBorderStyle);
            }
 
            CheckBoxState state = value != null && (bool)value ?
                CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
            Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
            Point center = new Point(cellBounds.X, cellBounds.Y);
            center.X += (cellBounds.Width - size.Width) / 2;
            center.Y += (cellBounds.Height - size.Height) / 2;
 
            CheckBoxRenderer.DrawCheckBox(graphics, center, state);
        }
        else
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                elementState, value, formattedValue, errorText,
                cellStyle, advancedBorderStyle, paintParts);
        }
    }
}

 使用:
 private void dgvView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

    if (e.RowIndex < 0 || e.ColumnIndex > colSelect.Index) { return; }

 

    DataRow dr = (dgvView.Rows[e.RowIndex].DataBoundItem as DataRowView)?.Row;

    if (dr != null)

    {

        string status= DataFun.ConvertToStringByDB(dr["status"]);

 

        if (status!= 'Doing')

        {

            DataGridViewDisableCheckBoxCell checkCell = (DataGridViewDisableCheckBoxCell)dgvView.Rows[e.RowIndex].Cells[colSelect.Index];

            if (checkCell == null) { return; }

 

            checkCell.Enabled = false;

            checkCell.ReadOnly = true;

        }

    }

}

 

posted @   威尔逊  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示