zoukankan      html  css  js  c++  java
  • Winfrom DateGridView 实现Button列禁用

    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);
                    }
                }
            }
        }
    }
    

      

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    【JS】修改字体
    【git】.gitignore文件常用设置
    【CSS】让textarea在div里水平垂直都居中的三种办法
    【JS】随着进度条进展,逐个显示li节点
    【MySQL】The server time zone value 'xxxxxx' is unrecognized or represents more than one time zone 解决方案
    浅谈Web前端开发中的Touch事件
    [转]PostgreSQL与MySQL比较
    RDLC:An error occurred during local report processing
    [转]让你的PHP更安全
    [转]Win7或Windows server 2008中IIS7支持ASP+Access解决方法
  • 原文地址:https://www.cnblogs.com/YYkun/p/5730004.html
Copyright © 2011-2022 走看看