locked
enabling/disabling buttons in datagridview with a button column RRS feed

  • Question

  • Hello,

    I have a datagridview with a button column. I want to enable/disable the buttons in this column programatically:

    if a flag f1 =1 then all the buttons in this column where there is data in this datagridview to be enabled alse dissabled

    if a flag f2 =1 then the new row to have its button enabled else disabled

    Please help

    Wednesday, September 19, 2012 9:45 AM

Answers

  • DataGridViewButtonCell can't be disabled and ReadOnly does not work for the DataGridView_CellContentClick. this is why in DataGridView_CellContentClick have to put the checking.

     If (e.ColumnIndex.ToString().Equals("7")) Then 
                           If (String.IsNullOrEmpty(DataGridView1.Rows(e.RowIndex).Cells(0).Value)) Then
    
                    If f2 = 1 Then
                        do necessary
                    Else
                        MessageBox.Show("You cant edit", "edit failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    If f1 = 1 Then
                        do necessary
                    Else
                        MessageBox.Show("You cant insert", "insert failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                    End If
                End If

    • Marked as answer by Barbi Rio Tuesday, September 25, 2012 7:46 AM
    Tuesday, September 25, 2012 7:45 AM

All replies

  • Hi,

    loop through all the rows of dgv, and set the appropriate column based on your flag:

    //class variable:
    int intFlag;
    
    //set it to an appropriate number (1, or 2) or what ever
    
    //inisde some method or event when enabling/disabling buttons:
    foreach(DataGridViewRow row in dgv.Rows)
    { 
        //your conditon if there is data in some column:
        if(!string.IsNullOrEmpty(row.Cells["DataColumn"].Value ).ToString()))
        { 
            DataGridViewButtonCell cell = row.Cells["ButtonColumnName"] as DataGridViewButtonCell;
            cell.Enabled = 1 ? false : true;  //if 1 - cell disabled, else enabled!
         }
    }

    Hope it helps,

    bye


    Mitja

    Wednesday, September 19, 2012 10:04 AM
  • There is no cell.Enabled
    Wednesday, September 19, 2012 10:46 AM
  • Ups, sorry, I was writing the code by heart, so I wasnt sure if Enabled exists or not. I checked and it does not exits, but what is does, its ReadOnly property.

    So to repair the code, it would look like:

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (!string.IsNullOrEmpty(row.Cells["DataColumnName"].Value.ToString()))
                    {
                        DataGridViewButtonCell cell = row.Cells["BottonColumnName"] as DataGridViewButtonCell;
                        if (cell != null)
                        {
                            cell.ReadOnly = 1 ? true : false; //if 1 cell will be disabled (not clickable)
                        }
                    }
                }


    Mitja

    Wednesday, September 19, 2012 1:05 PM
  • I implemeted it but it stilldoes not working

    It allows me to use the button even if readonly is true

    Friday, September 21, 2012 7:02 AM
  • Sorry, I did all too fast, but this code might work, try it out:

    //class variables:
    int flag1 =0;
    int flag2=0;
    
                //inside your loop:
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (flag1 != 1)
                        row.Cells["ButtonNameColumn"].ReadOnly = row.Cells["DataCoulumnName"].Value.ToString() != "" ? true : false;
                    if (flag2 != 1 && row.IsNewRow)
                        row.Cells["ButtonNameColumn"].ReadOnly = false;
                    else
                        row.Cells["ButtonNameColumn"].ReadOnly = false;
                }


    Mitja

    Friday, September 21, 2012 1:48 PM
  • I have try with row.Cells["ButtonColumName"].ReadOnly=true and also with  row.Cells[ButtonColumNumber].ReadOnly=true and I'm still getting this column enabled.
    Monday, September 24, 2012 5:34 AM
  • The problem is no matter if the cells are enabled or disabled the event DataGridView_CellContentClick

    Please advice if there is another way or what can be causing my problem

    Tuesday, September 25, 2012 5:08 AM
  • DataGridViewButtonCell can't be disabled and ReadOnly does not work for the DataGridView_CellContentClick. this is why in DataGridView_CellContentClick have to put the checking.

     If (e.ColumnIndex.ToString().Equals("7")) Then 
                           If (String.IsNullOrEmpty(DataGridView1.Rows(e.RowIndex).Cells(0).Value)) Then
    
                    If f2 = 1 Then
                        do necessary
                    Else
                        MessageBox.Show("You cant edit", "edit failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    If f1 = 1 Then
                        do necessary
                    Else
                        MessageBox.Show("You cant insert", "insert failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                    End If
                End If

    • Marked as answer by Barbi Rio Tuesday, September 25, 2012 7:46 AM
    Tuesday, September 25, 2012 7:45 AM