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. C/C++中的隐藏依赖

    转载自:http://trarck.googlecode.com/svn/trunk/article/C/%E9%9A%90%E8%97%8F%E4%BE%9D%E8%B5%96.txt 隐藏依赖:即 ...

  2. sql server 内置ETL工具学习(一) BCP篇

    sql server 内置ETL工具学习 常用的导入方式:bcp, BULK INSERT,OPENROWSET和 SSIS. BCP BCP全称BULK COPY PROGRAM 有以下特点: 命令 ...

  3. java中replace和replaceAll的区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharS ...

  4. 解读vmstat中的ACTIVE/INACTIVE MEMORY

    vmstat 命令能够报告关于内核线程.虚拟内存.磁盘.陷阱和 CPU 活动的统计信息,那么我们又该如何理解其工作原理呢? vmstat -a 命令能看到active memory 和 inactiv ...

  5. ZPPR016-在制品清单报表

    *&---------------------------------------------------------------------**& Report ZPPR016*&a ...

  6. 原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y【转】

    关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种 event.clientX/Y event.pageX/Y event.offsetX/Y event.layerX/Y event.sc ...

  7. 前端攻城狮学习笔记九:让你彻底弄清offset

    很多初学者对于JavaScript中的offset.scroll.client一直弄不明白,虽然网上到处都可以看一张图(图1),但这张图太多太杂,并且由于浏览器差异性,图示也不完全正确. 图一 不知道 ...

  8. uva562 Dividing coins 01背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. leetcode 122. Best Time to Buy and Sell Stock II ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. eclipse adt 项目依赖,使用git上的项目

    从git下载的android工程不能直接导入eclipse,有的项目要依赖这个项目,怎么办呢 好像只能新建一个android项目,然后把libs,res,src,AndroidManifest.xml ...