Form窗体如下所示:
实现如下:
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); } } } } }
时间: 2024-10-05 07:02:41