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);
}
}
}
}
}
Winfrom DateGridView 实现Button列禁用的更多相关文章
- 关于bootstrap--表单(按钮<button>效果、大小、禁用)
1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...
- 【Excle数据透视表】如何禁用数据透视表的总计行/列
如上图:有行合计也有列合计.现在我们需要将行列合计都去除,如何操作呢? 解决办法一: 数据透视表区域任意单元格→数据透视表工具→设计→布局→总计→对行和列禁用 解决办法二: 数据透视表区域任意单元格→ ...
- .NET组件控件实例编程系列——5.DataGridView数值列和日期列
在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...
- button swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- swift button一些简单设置
1,按钮的创建(1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDiscl ...
- Bootstrap的js插件之按钮(button)
1)属性: data-loading-text="载入中..."--使button呈现载入状态: data-toggle="button"--使按钮可以切换状态 ...
- Unity3D学习笔记(十九):UGUI、Image、Text、Button
UGUI:Unity官方最新,与NGUI同源 UI:User Interface(用户的操作界面),图片+文字 UGUI的组件: 1.创建UGUI组件时,会默认创建Canvas(画布)和EventSy ...
- jquery 禁用/启用滚动条
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bootstrap全局CSS样式之button和图片
.btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...
随机推荐
- codeforce Error Correct System
题目大意: 给出两串n(1 ≤ n ≤ 200 000)个字母的字符串, 求出最多交换一对数, 使得不相同对数变少,求出不相同的对数以及交换的数的位置,若不需交换则输出-1,-1. 分析: 用矩阵记录 ...
- codeforce The Art of Dealing with ATM
题目大意 ATM取款机有n种不同的钱币kind[i],每次取款允许吐出不超过k张钱币,且钱币的种类数不能超过2(一开始没理解2的意思),现在有q次取款,钱数为ques,问ATM能否凑出这样的钱,若能的 ...
- php 滑动验证码
自己研究: jQuery拖拽滑动验证码插件 slideunlock.js 原理:(别人说) 响应时间,拖拽速度,时间,位置,轨迹,重试次数等.这些因素能够构成一个采样结果或者辨识特性. 只获取到滑动时 ...
- ZOJ 1001 A + B Problem
熟悉ZOJ环境,如何上传代码,如何查看结果. #include<iostream> using namespace std; int main(){ int a,b; while(cin& ...
- HDU 2072 单词数
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2072 普通解法: /* HDU 2072 单词数 --- 字符串处理 */ #include < ...
- hdu3639 强连通
题意:有 n 个人,m 组支持关系,已知支持关系可以传递,比如 A 支持 B,则所有支持 A 的人也同时支持 B,问哪些人获得的支持数最多,最多获得多少支持(自己不能获得自己的支持). 首先,如果一些 ...
- hdu5443(2015长春赛区网络赛1007)暴力
题意:给了一个数列,有多个询问,每个询问求某个区间内的最大值 数列长度 1000,询问个数 1000,静态,并不需要RMQ这些,直接暴力 n2 查找每个询问区间取最大值就行了. #include< ...
- Tyvj 9.10 总结 (其实只是发一下心情)
原以为能拿200的,然而看到成绩彻底方了,60滚粗,连100都不到 这种成绩连一等都拿不到,我tm还谈什么省选,更别说NOI了 目前还没有部分分版本的,也没有数据,不知道问题出在哪里 tyvj群里好多 ...
- Visual Studio 2012 离线升级包的制作
通过CMD执行“VS2012.3.exe/layout”命令,就可以把在线升级的文件保存到本地,这样一来,只要把下载出来的文件打包上传,然后分享出去,所谓的“VS2012 Update 3 离线升级包 ...
- 论文笔记之:Multiple Feature Fusion via Weighted Entropy for Visual Tracking
Multiple Feature Fusion via Weighted Entropy for Visual Tracking ICCV 2015 本文主要考虑的是一个多特征融合的问题.如何有效的进 ...