Click here to Skip to main content
15,878,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an unbound checkbox column in my datagridview, added from the design. I want to select and highlight each row when i check any checkbox of corresponding row. For this i have wrote the following code:

C#
private void dgvUserData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            RowCheckBoxClick();
        }

private void RowCheckBoxClick()
        {
            int count = 0;
            foreach (DataGridViewRow row in dgvUserData.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    row.Selected = true;
                    count = count + 1;d
                }
                else
                {
                    row.Selected = false;
                    count = count - 1;
                }
            }
            
            //txtRecordSelected.Text = count.ToString();
        }


But it is behaving totally unexpected way. While debugging I noticed, the Convert.ToBoolean(row.Cells[0].Value) value is coming as null for checkboxes which i checked. I tried to place the RowCheckBoxClick() event in other events also, but none of them is working properly.
But surprisingly when I am clicking any other button on the screen, then the datagridview is showing with properly row highlighted or selected.
Am i missing anything? Any suggestion would be appreciated.
Posted
Comments
Why have you tagged the question with "ASP.NET"?

1 solution

try with both CellValueChanged and CurrentCellDirtyStateChanged events

C#
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {
        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
    }
}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}



Quote:
The DataGridView.CellValueChanged event occurs when the user-specified value is committed, which typically occurs when focus leaves the cell.
In the case of check box cells, however, you will typically want to handle the change immediately. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
Rows in the control are not automatically sorted when a cell value is changed. To sort the control when the user modifies a cell, call the Sort method in a CellValueChanged event handler.


http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx[^]
 
Share this answer
 
v4
Comments
Member 10758061 21-Apr-14 4:36am    
@Damith Weerasinghe Thanks for the solution..It is working now, the only thing is the grid is flickering a bit while checking/unchecking the checkboxes (like while I am checking new row in the grid, it is un-highlighting the previously checked row and then highlighting finally).

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



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