禁用datagridview中的按钮列

sre*_*ree 11 c# datagridview

我有一个数据网格视图有4列,前两列是组合框列,第三列是文本框列,第四列是按钮列.在表单加载我必须禁用datagrid的整个按钮列,在此之后我应该选择前三列和保存后,将这三个列保存在数据库中,特定行中的按钮列应该启用.应该通过单击按钮将第一列保存在数据库中.请帮助我解决这个问题很多天这里是我使用的代码

private void SATAddTemplate_Load(object sender, EventArgs e)
{
           foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
           {

               DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
               btn.ReadOnly = true;
           }
}
 private void btnSaveSettings_Click(object sender, EventArgs e)
     {
           foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
           {

               DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
               btn.ReadOnly = false;
           }
     }
Run Code Online (Sandbox Code Playgroud)

Jay*_*ggs 19

这里有一些帮助设置Enabled出现在按钮中的按钮的属性的问题DataGridViewButtonColumn.

您需要扩展DataGridViewButtonColumn以使用可禁用按钮创建自己的DataGridView列. 这篇关于MSDN的文章详细介绍了如何做到这一点.

这篇文章有很多代码,我建议你仔细看看,但你真正需要做的就是将文章中的以下类复制并粘贴到你的项目中:
- DataGridViewDisableButtonColumn
- DataGridViewDisableButtonCell

完成此操作后,您就可以将DataGridViewDisableButtonColumns 添加到DataGridView中.使用Enabled自定义列中公开的公共属性来设置Enabled每个单元格的Button控件的属性.由于您要设置Enabled列中所有按钮的属性,因此您可以编写一个辅助方法,循环遍历DataGridView中的所有行并设置Enabled属性:

private void SetDGVButtonColumnEnable(bool enabled) {
    foreach (DataGridViewRow row in dataGridView1.Rows) {
        // Set Enabled property of the fourth column in the DGV.
        ((DataGridViewDisableButtonCell)row.Cells[3]).Enabled = enabled;
    }
    dataGridView1.Refresh();
}
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么不能默认禁用按钮? (10认同)
  • MSDN 文章中的“DataGridViewDisableButtonCell.Paint()”方法在重绘时闪烁(无双缓冲)。使用 [这篇文章](https://msdn.microsoft.com/en-us/library/ka0yazs1(v=vs.110).aspx) 作为指南来补救修改他们的实现。创建一个BufferedGraphics对象(例如`myBuffer`),将`graphics`的用法替换为`myBuffer.Graphics`,最后调用`myBuffer.Render()`。警告:调用`TextRenderer.DrawText()` 必须指定这些文本格式标志以正确定位按钮文本:`PreserveGraphicsTranslateTransform | 水平中心 | 垂直中心` (2认同)

Chr*_*ley 10

这是杰伊回答的补充.

根据请求,这是我用来创建可以禁用的按钮单元格的代码.它包括双缓冲,以便在用户滚动时按钮不会闪烁.

/// <summary>
/// Adapted from https://msdn.microsoft.com/en-us/library/ms171619.aspx. Double-buffering was added to remove flicker on re-paints.
/// </summary>
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
    private bool enabledValue;

    public bool Enabled
    {
        get { return enabledValue; }
        set
        {
            if (enabledValue == value) return;
            enabledValue = value;
            // force the cell to be re-painted
            if (DataGridView != null) DataGridView.InvalidateCell(this);
        }
    }

    // Override the Clone method so that the Enabled property is copied. 
    public override object Clone()
    {
        var cell = (DataGridViewDisableButtonCell) base.Clone();
        cell.Enabled = Enabled;
        return cell;
    }

    // By default, enable the button cell. 
    public DataGridViewDisableButtonCell()
    {
        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)
    {
        // The button cell is disabled, so paint the border, background, and disabled button for the cell. 
        if (!enabledValue)
        {
            var currentContext = BufferedGraphicsManager.Current;

            using (var myBuffer = currentContext.Allocate(graphics, cellBounds))
            {
                // Draw the cell background, if specified. 
                if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                {
                    using (var cellBackground = new SolidBrush(cellStyle.BackColor))
                    {
                        myBuffer.Graphics.FillRectangle(cellBackground, cellBounds);
                    }
                }

                // Draw the cell borders, if specified. 
                if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
                {
                    PaintBorder(myBuffer.Graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
                }

                // Calculate the area in which to draw the button.
                var buttonArea = cellBounds;
                var buttonAdjustment = BorderWidths(advancedBorderStyle);
                buttonArea.X += buttonAdjustment.X;
                buttonArea.Y += buttonAdjustment.Y;
                buttonArea.Height -= buttonAdjustment.Height;
                buttonArea.Width -= buttonAdjustment.Width;

                // Draw the disabled button.                
                ButtonRenderer.DrawButton(myBuffer.Graphics, buttonArea, PushButtonState.Disabled);

                // Draw the disabled button text.  
                var formattedValueString = FormattedValue as string;
                if (formattedValueString != null)
                {
                    TextRenderer.DrawText(myBuffer.Graphics, formattedValueString, DataGridView.Font, buttonArea, SystemColors.GrayText, TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                }

                myBuffer.Render();
            }
        }
        else
        {
            // The button cell is enabled, so let the base class handle the painting. 
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)