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

  

Winfrom DateGridView 实现Button列禁用的更多相关文章

  1. 关于bootstrap--表单(按钮<button>效果、大小、禁用)

    1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...

  2. 【Excle数据透视表】如何禁用数据透视表的总计行/列

    如上图:有行合计也有列合计.现在我们需要将行列合计都去除,如何操作呢? 解决办法一: 数据透视表区域任意单元格→数据透视表工具→设计→布局→总计→对行和列禁用 解决办法二: 数据透视表区域任意单元格→ ...

  3. .NET组件控件实例编程系列——5.DataGridView数值列和日期列

    在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...

  4. button swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  5. swift button一些简单设置

    1,按钮的创建(1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDiscl ...

  6. Bootstrap的js插件之按钮(button)

    1)属性: data-loading-text="载入中..."--使button呈现载入状态: data-toggle="button"--使按钮可以切换状态 ...

  7. Unity3D学习笔记(十九):UGUI、Image、Text、Button

    UGUI:Unity官方最新,与NGUI同源 UI:User Interface(用户的操作界面),图片+文字 UGUI的组件: 1.创建UGUI组件时,会默认创建Canvas(画布)和EventSy ...

  8. jquery 禁用/启用滚动条

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Bootstrap全局CSS样式之button和图片

    .btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...

随机推荐

  1. iOS学习笔记---oc语言第一天

    第一讲 初始类和对象 c语言的超集,允许在oc中使用c语言源代码.编译器兼容c语言程序 具备完善的面向对象特性 包含一个运行时系统 类库丰富 面向对象编程 oop 面向对象语言:c++  java   ...

  2. vim 把满足条件的数字进行加上一些数字

    1,1,1,n4s-1 1,3,4,n3s= 1,4,6,e4h= 1,5,8,e4h-1 1,6,2,e3ntx-2 1,7,5,n1s+2 1,8,7,n3s=   比如以上的数据格式以“,”为列 ...

  3. HTML DOM部分---document对象;

    <style type="text/css"> #d3{ color:red} </style> </head> <body> &l ...

  4. c#部分---结构体再利用;

    //定义一个结构体,存放关于车辆的几个信息 //将所有车的信息都放入集合中 //车型号 价格(W) 轴距 (mm) 油耗(L/100km) //宝马320Li 38 2920 6.9 //宝马520L ...

  5. leetcode 97 Interleaving String ----- java

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. hihoCoder #1040 (判断是否为矩形)

    题目大意:给四条线段,问能否构成一个矩形? 题目分析:先判断能否构成四边形,然后选一条边,看另外三条边中是否为一条与他平行,两条垂直. 代码如下: # include<iostream> ...

  7. javaweb在线预览

    需要工具: 1.openoffice 将word.excel.ppt.txt等文件转换为pdf文件 2.SWFTool 将pdf转换为swf文件 3.flexPaper是一个开源轻量级的在浏览器上显示 ...

  8. JSBinding+SharpKit / MonoBehaviour替换成JSComponent原理

    Unity 是基于组件式的开发,gameObject 身上可以绑定任意个脚本.每个脚本组成 gameObject 的一个部分. 脚本里通过添加预定义好的函数来执行自己的任务.比如Awake,用于初始化 ...

  9. 利用zip(或者phar)协议进行本地文件包含

    $include_file=$_GET[include_file];if ( isset( $include_file ) && strtolower( substr( $includ ...

  10. sql server 如何在一个数据库中操作另一个数据库中的数据

    INSERT INTO T1 SELECT   * FROM      OPENDATASOURCE(          'SQLOLEDB',          'Data Source=Serve ...