Click here to Skip to main content
15,946,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking for how to enable butttons if checkbox is checked in gridview if no checkbox is checked ,button should be in disable mode. Below is my code details.Please help me.

What I have tried:

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {
           foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true)) //1 is the column number of checkbox
               {
                   row.Selected = true;
                   row.DefaultCellStyle.SelectionBackColor = Color.LightSlateGray;

               }
               else
                   row.Selected = false;

           }



           BtnCancel.Enabled = false;
           btnDelete.Enabled = false;
           btnUpdate.Enabled = false;
       }

Here in Datagridview Cell click I have turned off all the buttons

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{


// For this demo we will make exception for checkBoxColumn itself.
// So it is always editable.
if (e.ColumnIndex != chkbox.Index)
{
object rawValue = dataGridView1[chkbox.Index, e.RowIndex].Value;
bool value = rawValue is bool ? (bool)rawValue : false;
// But if checkBoxColumn is checked then editing not allowed
if (!value)
{
e.Cancel = true;
}

}




}
This BeginEdit enable me to edit any cells checkbox is checked else it won't allow me to edit any cells without checking in Checkbox
Posted
Updated 14-Jun-22 10:26am
Comments
[no name] 8-Jun-22 0:15am    
Probably start with the (row) SelectionChanged event.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.selectionchanged?view=windowsdesktop-6.0
Member 15627495 8-Jun-22 1:24am    
If you populate dynamically your datagridview,
at each loop add an event to the checkbox.

then on event checkbox click
gather all the checkbox checkedstate
and enabled or not the button area.

1 solution

I don't know how it will perform in the end, but you can easily use Linq as well. Place this code in e.g. the CellEndEdit event:

BtnCancel.Enabled = dataGridView1.Rows
                      .Cast<DataGridViewRow>()
                      .Select(a => a.Cells[0].Value as bool?)
                      .Count(a => a.HasValue && a.Value) > 0;


First the rows are casted as an IEnumerable<datagridviewrow> allowing the subsequent .Select to allow selecting the .Cells property of the row. Then select the first cell and cast its value as bool?. When the cell checkbox is checked its value is True, but when it is unchecked it returns null. Next .Count all cells that have a value and with value is True.

When the count of these cells > 0, the BtnCancel.Enabled is True.

You need to tweak it of course for your use case, but this should work. Also be aware that I did not test this for performance, but at the time this runs slow, you might need to consider refactoring anyhow :)
 
Share this answer
 

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

  Print Answers RSS


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