欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

Form窗体如下所示:

实现如下:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
 
namespace DataGridView添加禁用Button列
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        public List<User> GetAllUser()
        {
            List<User> UserList = new List<User>();
 
            User u = new User();
            u.UserID = Guid.NewGuid().ToString().Replace("-", "");
            u.UserName = "张三丰";
            u.UserType = 0;
            UserList.Add(u);
             
            User u1 = new User();
            u1.UserID = Guid.NewGuid().ToString().Replace("-", "");
            u1.UserName = "周芷若";
            u1.UserType = 1;
            UserList.Add(u1);
 
            User u2 = new User();
            u2.UserID = Guid.NewGuid().ToString().Replace("-", "");
            u2.UserName = "赵敏";
            u2.UserType = 1;
            UserList.Add(u2);
 
            User u3 = new User();
            u3.UserID = Guid.NewGuid().ToString().Replace("-", "");
            u3.UserName = "张无忌";
            u3.UserType = 0;
            UserList.Add(u3);
 
            return UserList;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            this.dgvUser.AutoGenerateColumns = false;
 
            List<User> UserList = GetAllUser();
            this.dgvUser.DataSource = UserList;
 
            string num = string.Empty;
            int count = this.dgvUser.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                num = this.dgvUser.Rows[i].Cells["UserType"].FormattedValue.ToString();
                DataGridViewDisableButtonCell btnCell = (DataGridViewDisableButtonCell)
                      this.dgvUser.Rows[i].Cells["UserStatus"];
                if (num.Equals("0"))
                {
                    btnCell.Enabled = false;
                }
                else btnCell.Enabled = true;
            }
        }
 
        private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            string colName = this.dgvUser.Columns[e.ColumnIndex].Name;
            if (colName.Equals("UserStatus")) //设置
            {
                DataGridViewDisableButtonCell btnCell = (DataGridViewDisableButtonCell)
                    this.dgvUser.CurrentRow.Cells["UserStatus"];
                if (!btnCell.Enabled) return;
 
                MessageBox.Show("Test");
            }
        }
    }
 
    public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
    {
        public DataGridViewDisableButtonColumn()
        {
            this.CellTemplate = new DataGridViewDisableButtonCell();
        }
    }
 
    public class DataGridViewDisableButtonCell : DataGridViewButtonCell
    {
        private bool enabledValue;
        public bool Enabled
        {
            get
            {
                return enabledValue;
            }
            set
            {
                enabledValue = value;
            }
        }
 
        public override object Clone()
        {
            DataGridViewDisableButtonCell cell =
                (DataGridViewDisableButtonCell)base.Clone();
            cell.Enabled = this.Enabled;
            return cell;
        }
 
        public DataGridViewDisableButtonCell()
        {
            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 (rowIndex != -1)
            {
                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);
                    }
                    Rectangle buttonArea = cellBounds;
                    Rectangle buttonAdjustment =
                        this.BorderWidths(advancedBorderStyle);
                    buttonArea.X += buttonAdjustment.X;
                    buttonArea.Y += buttonAdjustment.Y;
                    buttonArea.Height -= buttonAdjustment.Height;
                    buttonArea.Width -= buttonAdjustment.Width;
                    ButtonRenderer.DrawButton(graphics, buttonArea,
                        System.Windows.Forms.VisualStyles.PushButtonState.Disabled);
 
                    if (this.FormattedValue is String)
                    {
                        TextRenderer.DrawText(graphics, (string)this.FormattedValue,
                            this.DataGridView.Font, buttonArea, SystemColors.GrayText);
                    }
                }
                else
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                        elementState, value, formattedValue, errorText,
                        cellStyle, advancedBorderStyle, paintParts);
                }
            }
        }
    }
}

  

posted on   sunwugang  阅读(795)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示