[C#]禁用DataGridView中的按钮 [英] [C#] Disable Button in DataGridView

查看:448
本文介绍了[C#]禁用DataGridView中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
搜索文章后,我想禁用DataGridView中的按钮:
http://stackoverflow.com/questions/12525305/disabling-the-button- datagridview中的列
但是,我仍然无法实现...
希望有人给我一个建议!
非常感谢!

Hi, everyone!
I want to disable the button in DataGridView, having searched the article:
http://stackoverflow.com/questions/12525305/disabling-the-button-column-in-the-datagridview
However, I still couldn''t fulfill it...
Hope somebody would give me a suggestion!
Thanks a lot!!

using System.Windows.Forms.VisualStyles;
//my code
DataTable dtDGV1 = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
            dtDGV1.Columns.Add("A");
            dtDGV1.Columns.Add("B");
            dtDGV1.Columns.Add("C");
            
            dtDGV1.Rows.Add("a", "b", "c");
            dtDGV1.Rows.Add("aa", "bb", "cc");

            dataGridView1.DataSource = dtDGV1; //Paste the DataTable to DGV

            //Create the DGV button 
            DataGridViewButtonColumn uninstallButtonColumn = new        DataGridViewButtonColumn();
            uninstallButtonColumn.Name = "Button";
            uninstallButtonColumn.Text = "Set";
            uninstallButtonColumn.UseColumnTextForButtonValue = true;
            dataGridView1.Columns.Insert(3, uninstallButtonColumn);

            SetDGVButtonColumnEnable(false);  //disable the DGV button
            <big>//But It cannot active...</big>
}

//class and method from article
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;
                }
            }

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

            // By default, enable the button 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)
            {
                // The button cell is disabled, so paint the border,   
                // background, and disabled button for the cell. 
                if (!this.enabledValue)
                {
                    // Draw the cell background, if specified. 
                    if ((paintParts & DataGridViewPaintParts.Background) ==
                        DataGridViewPaintParts.Background)
                    {
                        SolidBrush cellBackground =
                            new SolidBrush(cellStyle.BackColor);
                        graphics.FillRectangle(cellBackground, cellBounds);
                        cellBackground.Dispose();
                    }

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

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

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

                    // Draw the disabled button text.  
                    if (this.FormattedValue is String)
                    {
                        TextRenderer.DrawText(graphics,
                            (string)this.FormattedValue,
                            this.DataGridView.Font,
                            buttonArea, SystemColors.GrayText);
                    }
                }
                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);
                }
            }
}

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

推荐答案

感谢Hajnal的帮助,以至于我忘了将DataGridViewButtonColumn换成DataGridViewDisableButtonColumn.

〜规格〜
您只需要从我的代码中更改行即可完成DataGridView中的DISABLE按钮!

正常:
DataGridViewButtonColumn uninstallButtonColumn =新的DataGridViewButtonColumn();
成为:
DataGridViewDisableButtonColumn uninstallButtonColumn =
新的DataGridViewDisableButtonColumn();

再次感谢Hajnal:)
Thanks for Hajnal''s help, so that I can find I forget to exchange the DataGridViewButtonColumn for DataGridViewDisableButtonColumn.

~Specification~
You just need to change the line from my code, and it can accomplish the DISABLE button in DataGridView!

normal:
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
become:
DataGridViewDisableButtonColumn uninstallButtonColumn =
new DataGridViewDisableButtonColumn();

Thanks again, Hajnal:)


这篇关于[C#]禁用DataGridView中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆