首页 > 其他分享 >DataGridView DataGridViewCheckBoxColumn 实现禁用效果,因为默认的不带禁用效果

DataGridView DataGridViewCheckBoxColumn 实现禁用效果,因为默认的不带禁用效果

时间:2024-09-23 09:04:06浏览次数:1  
标签:DataGridViewCheckBoxColumn cellBounds DataGridViewPaintParts 禁用 DataGridView Dat

/// <summary>
/// DataGridViewDisableCheckBoxColumn 
/// 自定义disablecheckbox列 实现禁用效果
/// </summary>
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
    public DataGridViewDisableCheckBoxColumn()
    {
        this.CellTemplate = new DataGridViewDisableCheckBoxCell();
    }
}

public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
{
    private bool enabledValue;

    public bool Enabled
    {
        get
        {
            return enabledValue;
        }
        set
        {
            enabledValue = value;
        }
    }

    public override object Clone()
    {
        DataGridViewDisableCheckBoxCell cell =
            (DataGridViewDisableCheckBoxCell)base.Clone();
        cell.Enabled = this.Enabled;
        return cell;
    }

    public DataGridViewDisableCheckBoxCell()
    {
        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 (!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);
            }

            CheckBoxState state = value != null && (bool)value ?
                CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;
            Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
            Point center = new Point(cellBounds.X, cellBounds.Y);
            center.X += (cellBounds.Width - size.Width) / 2;
            center.Y += (cellBounds.Height - size.Height) / 2;

            CheckBoxRenderer.DrawCheckBox(graphics, center, state);
        }
        else
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                elementState, value, formattedValue, errorText,
                cellStyle, advancedBorderStyle, paintParts);
        }
    }
}

 使用:
 private void dgvView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

    if (e.RowIndex < 0 || e.ColumnIndex > colSelect.Index) { return; }

 

    DataRow dr = (dgvView.Rows[e.RowIndex].DataBoundItem as DataRowView)?.Row;

    if (dr != null)

    {

        string status= DataFun.ConvertToStringByDB(dr["status"]);

 

        if (status!= 'Doing')

        {

            DataGridViewDisableCheckBoxCell checkCell = (DataGridViewDisableCheckBoxCell)dgvView.Rows[e.RowIndex].Cells[colSelect.Index];

            if (checkCell == null) { return; }

 

            checkCell.Enabled = false;

            checkCell.ReadOnly = true;

        }

    }

}

 

 

/// <summary>/// DataGridViewDisableCheckBoxColumn /// 自定义disablecheckbox列 实现禁用效果/// </summary>/* Started by AICoder, pid:pe484e7c5fz4fdf14e3d08f8e0bfa38107705bcf */public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn{    public DataGridViewDisableCheckBoxColumn()    {        this.CellTemplate = new DataGridViewDisableCheckBoxCell();    }}
public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell{    private bool enabledValue;
    public bool Enabled    {        get        {            return enabledValue;        }        set        {            enabledValue = value;        }    }
    public override object Clone()    {        DataGridViewDisableCheckBoxCell cell =            (DataGridViewDisableCheckBoxCell)base.Clone();        cell.Enabled = this.Enabled;        return cell;    }
    public DataGridViewDisableCheckBoxCell()    {        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 (!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);            }
            CheckBoxState state = value != null && (bool)value ?                CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled;            Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);            Point center = new Point(cellBounds.X, cellBounds.Y);            center.X += (cellBounds.Width - size.Width) / 2;            center.Y += (cellBounds.Height - size.Height) / 2;
            CheckBoxRenderer.DrawCheckBox(graphics, center, state);        }        else        {            base.Paint(graphics, clipBounds, cellBounds, rowIndex,                elementState, value, formattedValue, errorText,                cellStyle, advancedBorderStyle, paintParts);        }    }}

标签:DataGridViewCheckBoxColumn,cellBounds,DataGridViewPaintParts,禁用,DataGridView,Dat
From: https://www.cnblogs.com/Wilson6/p/18426282

相关文章

  • 帝国网站后台密码忘了,无法修改,提示您的用户名、密码或安全答案有误,也可能您的帐号已被
    如果你在尝试重置帝国CMS后台密码时遇到了“您的用户名、密码或安全答案有误,也可能您的账号已被禁用”的提示,这可能是因为你的账户状态存在问题,或者是由于数据库中的某些字段需要特别处理才能完成密码重置。以下是一些可能的解决方案:步骤一:检查账号状态检查账号是否被禁用在p......
  • 帝国cms 您的用户名、密码或安全答案有误,也可能您的帐号已被禁用,请重输
    当你忘记了帝国CMS的后台密码时,可以通过直接修改数据库中的用户信息来重置密码。以下是重置密码的步骤:步骤1:进入phpMyAdmin首先,你需要登录到你的服务器,并通过phpMyAdmin或者其他的数据库管理工具访问你的数据库。步骤2:找到phome_enewsuser数据表在数据库管理工具中找到存储用......
  • VBA技术资料MF198:禁用下拉拖放
    我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。“VBA语言攻”提供的教程一共九套,分为初级、中级、高级三大部分,教程是对VBA的系统讲解,从简单的入门、到入门后的提高、到数据库、到字典、到WordVBA的应用、到高级......
  • VBA技术资料MF197:禁用复制的快捷键
    我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。“VBA语言攻”提供的教程一共九套,分为初级、中级、高级三大部分,教程是对VBA的系统讲解,从简单的入门、到入门后的提高、到数据库、到字典、到WordVBA的应用、到高级......
  • 禁止程序运行的5种方法,一分钟学会禁用程序
    禁止程序运行可以通过多种方式实现,主要根据不同的操作系统和需求来选择合适的工具和方法。以下是几种常见的方法和工具,用于限制或禁止程序在电脑上运行:1. 使用Windows组策略(GroupPolicy)适用于Windows专业版及以上版本,组策略允许用户设置规则,禁止特定程序运行。步骤:打......
  • 安卓玩机工具-----无需root权限 卸载 禁用 删除当前机型app应用 ADB玩机工具
       ADB玩机工具                   ADBAppControl是很实用的安卓手机应用管理工具,无需root权限,通过usb连接电脑后,可以很方便的进行应用程序安装与卸载,还支持提取手机应用apk文件到电脑上,此外还有手机系统垃圾清理、上传文件等,功......
  • TS中除了这些情况请禁用any
    在TypeScript中,any类型是一个非常强大的类型。它允许你将一个值当作在JavaScript中一样来处理,而不是在TypeScript中。这意味着它禁用了TypeScript的所有特性,包括类型检查、自动补全和安全性。constmyFunction=(input:any)=>{input.someMethod();};myFunction("abc"......
  • PbootCMS在阿里云主机上邮件发送失败:服务器已经禁用stream_socket_client和fsockopen
    当你遇到网站的留言邮件通知功能失效,并且在测试发送邮件时收到“服务器已经禁用stream_socket_client和fsockopen函数,请至少开启一个才能发送邮件!”这样的错误提示时,这通常意味着服务器出于安全考虑禁用了这两个PHP函数。这两个函数是用来建立网络连接的,对于发送邮件等功能至关重......
  • dotnet 禁用 SQLite 的 SQLiteFunction 扫描程序集提升启动性能
    在我所在的团队开发的一个WPF应用程序里面,使用到了SQLite作为本地数据库。在优化启动性能过程中,发现了在启动过程一旦访问SQLite将会因为SQLiteFunction扫描程序集导致CPU损耗,从而影响启动性能。本文将告诉大家如何禁用SQLite的SQLiteFunction扫描程序集在SQLiteF......
  • Android 12.0 Launcher3禁用widget微件功能实现
    1.前言在12.0的系统rom定制化开发中,在一些Launcher3的定制化功能中,有些产品禁用appwidget微件功能,要求Launcher去掉加载widget微件功能,接下来具体分析下widget微件的加载流程2.Launcher3禁用widget微件功能实现的核心类packages/apps/Launcher3/src/com/android/launcher3/......