• C# WinForm DataGridView DataGridViewButtonColumn列禁用


    先添加一下两个类

    #region 禁用 DataGridViewButtonColumn
    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 (!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);
    }
    }
    }
    #endregion

    若DataGridView 已绑定数据使用以下方法

    private void DisableButtonCell(DataGridView dgv,string strKey)
    {
    for (int j = 0; j < dgv.RowCount; j++)
    {
    if (dgv.Rows[j].Cells[strKey].Value.ToString() == "要禁用的数据值")
    {
    if (dgv.Rows[j].Cells[strKey] is DataGridViewDisableButtonCell)
    ((DataGridViewDisableButtonCell)dgv.Rows[j].Cells[strKey]).Enabled = false;    //禁用

    }
    }
    }

  • 相关阅读:
    Windows Phone 7 立体旋转动画的实现
    jQuery 表格Table插件汇总
    SNS社交类网站照片头像裁剪源码
    VS无法启动调试
    SQL Server中获取第一天、最后一天
    jQuery技巧总结
    IT人士应当知道的10个行业小内幕
    巧用SQL server临时表
    将Html文档整理为规范XML文档
    16个Javascript的Web UI库、框架及工具包
  • 原文地址:https://www.cnblogs.com/hefy/p/7698526.html
Copyright © 2020-2023  润新知