lundi 18 septembre 2023

A Checkbox and a court column is within a gridview problem

The problem is I have a lot of rows in gridview but when I checked a certain or any Checkbox, I wasn't able to add 1 in count column.

I just need a code that whenever in any row of gridview I check a specific checkbox it will automatically add 1 to corresponding count value

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Check if the clicked cell is in the "Attendance" column
    if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Attendance"].Index)
    {
        // Toggle the "Attendance" value in the "Attend" column
        DataGridViewCheckBoxCell checkBoxCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

        if (checkBoxCell != null) // Check if the cell exists
        {
            // Get the current value of the "Attend" cell (assuming it contains numeric data)
            int currentAttendance = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["Attend"].Value);

            // Toggle the value between "1" and "0" when the checkbox is clicked
            int newAttendance = (currentAttendance == 0) ? 1 : 0;

            // Update the "Attend" cell with the new value
            dataGridView1.Rows[e.RowIndex].Cells["Attend"].Value = newAttendance;
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire