none
Remove/Hide button on datagridview

    Question

  • Hello all,

    I am using VS 2005 with .Net 2.0. I've got a databound datagridview on a form, with an unbound buttoncolumn as the last column of the grid.

    As not every row of the datagridview should be editable (some contain sub-headers), how do I remove or hide the button on the buttoncolumn for those rows?

    Thanks.
    Wednesday, January 07, 2009 12:26 PM

Answers

  • Hi Alf928,

     

    If we want to remove or hide the button cell in the DataGridView, we need to create a custom DataGridViewButtonColumn and extend the DataGridViewButtonCell to implement a visual “hide” state along with a corresponding hide property.

     

    This is similiar with the FAQ15: How do I disable a cell?  And We need a little change if we don't want to show the button.

     

    Please try the following code:

     

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                DataGridViewDisableButtonColumn disbtn = new DataGridViewDisableButtonColumn();

     

                this.dataGridView1.Columns.Add(disbtn);

     

                this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());

                this.dataGridView1.Rows.Add(5);

     

                ((DataGridViewDisableButtonCell)this.dataGridView1[0, 0]).Hide = true;

     

            }

        }

     

        public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn

        {

     

            public DataGridViewDisableButtonColumn()

            {

     

                this.CellTemplate = new DataGridViewDisableButtonCell();

     

            }

     

        }

     

     

     

        public class DataGridViewDisableButtonCell : DataGridViewButtonCell

        {

     

            private bool hideValue;

     

            public bool Hide

            {

     

                get

                {

     

                    return hideValue;

     

                }

     

                set

                {

     

                    hideValue = value;

     

                }

     

            }

     

     

     

            // Override the Clone method so that the Hide property is copied.

     

            public override object Clone()

            {

     

                DataGridViewDisableButtonCell cell =

     

                    (DataGridViewDisableButtonCell)base.Clone();

     

                cell.Hide = this.Hide;

     

                return cell;

     

            }

     

     

     

            // By default, display the button cell.

     

            public DataGridViewDisableButtonCell()

            {

     

                this.hideValue = false;

     

            }

     

     

     

            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 hide, so paint the border, 

     

                // background, and hide button for the cell.

     

                if (this.hideValue)

                {

     

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

     

                    }

     

     

                    // if we don't want to draw the button at all, comment out the following lines.

     

     

                    // 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 isn't hide, so let the base class

     

                    // handle the painting.

     

                    base.Paint(graphics, clipBounds, cellBounds, rowIndex,

     

                        elementState, value, formattedValue, errorText,

     

                        cellStyle, advancedBorderStyle, paintParts);

     

                }

     

            }

     

        }

     

    If you have any other questions, please feel free to ask.

     

    Best regards,

    Ling


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Tuesday, January 13, 2009 6:20 AM
    Saturday, January 10, 2009 12:03 PM
    Moderator

All replies

  • Hi Alf928,

     

    If we want to remove or hide the button cell in the DataGridView, we need to create a custom DataGridViewButtonColumn and extend the DataGridViewButtonCell to implement a visual “hide” state along with a corresponding hide property.

     

    This is similiar with the FAQ15: How do I disable a cell?  And We need a little change if we don't want to show the button.

     

    Please try the following code:

     

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                DataGridViewDisableButtonColumn disbtn = new DataGridViewDisableButtonColumn();

     

                this.dataGridView1.Columns.Add(disbtn);

     

                this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());

                this.dataGridView1.Rows.Add(5);

     

                ((DataGridViewDisableButtonCell)this.dataGridView1[0, 0]).Hide = true;

     

            }

        }

     

        public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn

        {

     

            public DataGridViewDisableButtonColumn()

            {

     

                this.CellTemplate = new DataGridViewDisableButtonCell();

     

            }

     

        }

     

     

     

        public class DataGridViewDisableButtonCell : DataGridViewButtonCell

        {

     

            private bool hideValue;

     

            public bool Hide

            {

     

                get

                {

     

                    return hideValue;

     

                }

     

                set

                {

     

                    hideValue = value;

     

                }

     

            }

     

     

     

            // Override the Clone method so that the Hide property is copied.

     

            public override object Clone()

            {

     

                DataGridViewDisableButtonCell cell =

     

                    (DataGridViewDisableButtonCell)base.Clone();

     

                cell.Hide = this.Hide;

     

                return cell;

     

            }

     

     

     

            // By default, display the button cell.

     

            public DataGridViewDisableButtonCell()

            {

     

                this.hideValue = false;

     

            }

     

     

     

            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 hide, so paint the border, 

     

                // background, and hide button for the cell.

     

                if (this.hideValue)

                {

     

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

     

                    }

     

     

                    // if we don't want to draw the button at all, comment out the following lines.

     

     

                    // 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 isn't hide, so let the base class

     

                    // handle the painting.

     

                    base.Paint(graphics, clipBounds, cellBounds, rowIndex,

     

                        elementState, value, formattedValue, errorText,

     

                        cellStyle, advancedBorderStyle, paintParts);

     

                }

     

            }

     

        }

     

    If you have any other questions, please feel free to ask.

     

    Best regards,

    Ling


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Tuesday, January 13, 2009 6:20 AM
    Saturday, January 10, 2009 12:03 PM
    Moderator
  • Hi Ling,

    I've used your code above to only display buttons in certain cells in a datagridview column, and it hides buttons correctly for cells where I don't want buttons to appear. However the grid's CellContentClick event has stopped working since I added the code - do you know why this might be? I put a break point in to the event but it doesn't fire when I click on the grid.

    Thanks,

    Ciaran.
    Thursday, January 14, 2010 4:03 PM