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的一 ...
随机推荐
- 单元测试-代码覆盖率工具 -- JaCoCo
最近学习Mybatis的官方文档,看到了[项目文档]一节有很多内容没有见过,做个笔记,理解一下. 随着敏捷开发的流行,编写单元测试已经成为业界共识.但如何来衡量单元测试的质量呢?有些管理者片面追求单元 ...
- 使用dropbox 同步任意文件夹(MAC)
使用dropbox自动同步自己mac上的文件夹: -My_Programs /Users/foreverycc/Dropbox/My_Program # . must be full absolute ...
- UI学习笔记---第十二天UITabBarController
页签视图控制器-UITabBarController 自定义UITabBar block高级 一.UITabBarController 结构为三层:Tab bar controller v ...
- 作业:用HTML制作邮箱登陆界面
<body leftmargin="200" rightmargin="200"> <font size="45" > ...
- DELPHI WEBSERVICE
一.服务程序 1.依次选择 NEW -> OTHER -> WEB SERVICE -> SOAP SERVER APPLICATION -> ISAPI DYNAMIC LI ...
- Reverse a Singly LinkedList
Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val ...
- Codeforces Round #339 Div.2 A - Link/Cut Tree
第一次正式参加常规赛想想有些小激动的呢 然后第一题就被hack了 心痛 _(:зゝ∠)_ tle点在于越界 因此结束循环条件从乘变为除 done //等等 这题没过总评 让我静静........ // ...
- Java Web学习(1): 客户端请求、服务器响应及其HTTP状态码
一JSP客户端请求 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信 息头的一部分来传送的.我们可以查阅HTTP协议来获得更多的信息. 下表列出了浏 ...
- 复利计算- 结对2.0--复利计算WEB升级版
客户在大家的引导下,有了更多的想法: 这个数据我经常会填.....帮我预先填上呗?...... 把界面做得简单漂亮好操作一点呗? 能不能帮我转成个APP,我装到手机上就更方便了? 我觉得这个很有用,很 ...
- 论文阅读之 DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation
DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation Xia ...