Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!!
C#
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();
}
Posted
Comments
Sinisa Hajnal 5-Nov-14 2:33am    
"I still couldn't..." Why?
You're adding " DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();" instead of DataGridViewDisableButtonColumn same for the cell...if you have them you should use them to be able to use their properties...right?
Member 10307999 5-Nov-14 5:13am    
Thanks for your suggestion! I finds my question!! So appreciated for your help!!!

1 solution

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:)
 
Share this answer
 
Comments
Sinisa Hajnal 5-Nov-14 5:56am    
No problem, glad to help.
Member 755230 6-Feb-20 2:26am    
Hi Hajnal - this code is working fine if I have the form seperately, but if my form is a child form, the DataGridViewDisableButtonColumn is not setting the enable property. is there a way to make the same code work if the form is a child form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900