【问题标题】:Disabling the button column in the datagridview禁用datagridview中的按钮列
【发布时间】:2012-09-13 13:37:43
【问题描述】:

我有一个 4 列的数据网格视图,前 2 列是组合框列,第三列是文本框列,第四列是按钮列。在表单加载中,我必须禁用数据网格的整个按钮列,然后我应该先选择三列并在保存后将前三列保存在数据库中,特定行中的按钮列应启用。前三列应通过单击按钮保存在数据库中。 请帮助我,我从很多天开始就遇到了这个问题 这是我使用的代码

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;
           }
     }

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

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

    您需要扩展 DataGridViewButtonColumn 以创建您自己的带有可禁用按钮的 DataGridView 列。 This article on MSDN 详细说明如何执行此操作。

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

    完成此操作后,您将能够将DataGridViewDisableButtonColumns 添加到您的DataGridView。使用自定义列中公开的Enabled 属性设置每个单元格的按钮控件的Enabled 属性。由于您要设置列中所有按钮的 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();
    }
    

    【讨论】:

    • 我想知道为什么不能在默认情况下禁用按钮?
    • MSDN 文章中的DataGridViewDisableButtonCell.Paint() 方法在重绘时闪烁(无双缓冲)。使用this article 作为指导来补救修改他们的实现。创建一个 BufferedGraphics 对象(例如myBuffer),将graphics 的用法替换为myBuffer.Graphics,然后在最后调用myBuffer.Render()。警告:对TextRenderer.DrawText() 的调用必须指定这些文本格式标志以正确定位按钮文本:PreserveGraphicsTranslateTransform | HorizontalCenter | VerticalCenter
    • 此链接中的解决方案也不适合您的 CellStyle 中的填充。您可以通过将 Padding.Horizo​​ntal 值添加到 buttonAdjustment.Width 并将 Padding.Vertical 值添加到 buttonAdjustment.Height 并将 cellStyle 的 Padding.Left 添加到 buttonAdjustment.X 和 cellStyle 的 Padding 来更改 Paint 方法以合并“cellStyle”中存在的任何 Padding .Top to buttonAdjustment.Y.
    • @ChrisStaley 我知道这是一个有点老的问题,但你可以分享代码来解决闪烁的问题吗?我无法让它工作。
    • @ChrisStaley 谢谢你。我会马上检查的!
    【解决方案2】:

    这是对杰的回答的补充。

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

    /// <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);
            }
        }
    }
    

    【讨论】:

    • 感谢您发布此代码,这是最好的答案!
    • 我正在使用它,但是当按钮被禁用而不在数据网格上检查它时,有什么方法可以停止读取点击?
    【解决方案3】:

    您可以使用这篇 MSDN 文章 MSDN article:Disable button in dataGridView 它使用一个用于 datagridview 按钮的类,并注意您必须在愿意处理它时检查按钮的启用状态

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2016-07-15
      • 2018-12-13
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多