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. JavaScript 编写多线程代码引用Concurrent.Thread.js(转)

    这是一个很简单的功能实现: <script type="text/javascript" src="Concurrent.Thread.js">&l ...

  2. c#实现数据的左补右补功能

    /// <summary>        /// 左補右補功能        /// </summary>        /// <param name="st ...

  3. 264. Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  4. linux中socket的理解---4

    一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打 开open –> 读写write/read –> 关闭close”模式来操作.So ...

  5. Windows下IntelliJ IDEA中运行Spark Standalone

    ZHUAN http://www.cnblogs.com/one--way/archive/2016/08/29/5818989.html http://www.cnblogs.com/one--wa ...

  6. UML开始,OOA与OOD以及一个网站

    1.Object-Oriented Analysis(面向对象分析方法)是确定需求或者业务的角度,按照面向对象的思想来分析业务 2.面向对象设计(Object-Oriented Design,OOD) ...

  7. understanding checkpoint_completion_target

    Starting new blog series – explanation of various configuration parameters. I will of course follow ...

  8. Q7: Unique Binary Search Trees

    问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...

  9. Flex 布局教程:语法篇(转)

    网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中 ...

  10. TKinter之菜单

    菜单的分类也较多,通常可以分为下拉菜单.弹出菜单. 1.使用Menu类创建一个菜单 2.add_command添加菜单项,如果该菜单是顶层菜单,则添加的菜单项依次向右添加. 如果该菜单时顶层菜单的一个 ...