none
Hiding a Button in a DataGridView

    Question

  •  
    I have a DataGridView in a windows form that I need to be able to make the datagridviewbutton visible or invisible depending on the value of another column. I can not figure out how to make this happen when I bind the datatable to the datagridview.

    Any help would be appreciated.

    Thanks

    John
    Wednesday, June 30, 2010 7:24 PM

Answers

  • Hi John,

    Based on my experience, there are two options to make the DataGridViewButtonCell invisible.

    One way is that we can custom paint the cell. Since the button inside a DataGridViewButtonCell is not an actual button, it is painted like a button, we can paint particular cell to make it looks like a common cell by handling the CellPainting event. Please check the following link for details.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting.aspx

    The other way is very tricky. We can change the DataGridViewButtonCell to DataGridViewTextBoxCell to hide the Button. Here is a sample for your information.

        public partial class Form17 : Form
        {
            public Form17()
            {
    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

                InitializeComponent();

            }

     

            private void Form17_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("id");

                dt.Columns.Add("item");

                for (int i = 0; i < 50; i++)

                {

                    dt.Rows.Add(i, "item" + i);

                }

                dt.AcceptChanges();

     

                DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();

                btnColumn.DataPropertyName = "id";

                btnColumn.Width = 100;

                btnColumn.ReadOnly = true;

                this.dataGridView1.Columns.Add(btnColumn);

                this.dataGridView1.DataSource = dt;

     

                foreach (DataGridViewRow dr in this.dataGridView1.Rows)

                {

                    if (dr.Cells[0].Value != null)

                    {

                        int i;

                        if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))

                        {

                            if (i % 2 == 0)

                            {

                                DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

                                dr.Cells[0] = txtcell;

                            }

                        }

                    }

                }

            }

        }

     

    Best regards,
    Helen Zhou

     


    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 Helen Zhou Wednesday, July 07, 2010 3:05 AM
    Tuesday, July 06, 2010 3:43 AM

All replies

  • Hi,

     

    You could handle the CellValueChanged of the DataGridVIiew,

    In this event you could check if the value of e.ColumnIndex is equal to the index of the condition you want, if true you can cast the datagridviewbutton cell and stablish the Visible property to True or false:

     

     


    Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
    Si la respuesta te ha sido util Votala.
    Mi Blog: Jtorrecilla
    Enlace a Faq de Winforms en Ingles Muy bueno
    Thursday, July 01, 2010 7:48 AM
  • Hi John,

    Based on my experience, there are two options to make the DataGridViewButtonCell invisible.

    One way is that we can custom paint the cell. Since the button inside a DataGridViewButtonCell is not an actual button, it is painted like a button, we can paint particular cell to make it looks like a common cell by handling the CellPainting event. Please check the following link for details.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting.aspx

    The other way is very tricky. We can change the DataGridViewButtonCell to DataGridViewTextBoxCell to hide the Button. Here is a sample for your information.

        public partial class Form17 : Form
        {
            public Form17()
            {
    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

                InitializeComponent();

            }

     

            private void Form17_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("id");

                dt.Columns.Add("item");

                for (int i = 0; i < 50; i++)

                {

                    dt.Rows.Add(i, "item" + i);

                }

                dt.AcceptChanges();

     

                DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();

                btnColumn.DataPropertyName = "id";

                btnColumn.Width = 100;

                btnColumn.ReadOnly = true;

                this.dataGridView1.Columns.Add(btnColumn);

                this.dataGridView1.DataSource = dt;

     

                foreach (DataGridViewRow dr in this.dataGridView1.Rows)

                {

                    if (dr.Cells[0].Value != null)

                    {

                        int i;

                        if ((int.TryParse(dr.Cells[0].Value.ToString(),out i)))

                        {

                            if (i % 2 == 0)

                            {

                                DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();

                                dr.Cells[0] = txtcell;

                            }

                        }

                    }

                }

            }

        }

     

    Best regards,
    Helen Zhou

     


    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 Helen Zhou Wednesday, July 07, 2010 3:05 AM
    Tuesday, July 06, 2010 3:43 AM