DataGridView checkbox selection

I added datagridview

win to my form app and also added CheckBox

for line marking. CheckBox

works as i expected until the user sorts datagridview

. After sorting, the previous selection of the checkbox column is lost.

Is there a way to make mine datagridview

remember which row is selected after sorting?

+2


a source to share


2 answers


You have two options for solving this problem.

The first and probably the simplest is to bind the checkbox column to your data source. For example, if you are using a DataTable as your data source, adding a boolean column will create a checkbox in your DataGridView that will sort and not lose checked state.

If this is not an option, then another way to work around the problem is to set the DataGridView mode to Virtual

and store a cache of your checkbox values.



Check out the excellent DataGridView FAQ for an example on how to do this. I've also provided the code below, but check out the FAQ:

private System.Collections.Generic.Dictionary<int, bool> checkState;
private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = customerOrdersBindingSource;

    // The check box column will be virtual.
    dataGridView1.VirtualMode = true;
    dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());

    // Initialize the dictionary that contains the boolean check state.
    checkState = new Dictionary<int, bool>();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // Update the status bar when the cell value changes.
    if (e.ColumnIndex == 0 && e.RowIndex != -1)
    {
        // Get the orderID from the OrderID column.
        int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
        checkState[orderID] = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
    }    
}

private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
    // Handle the notification that the value for a cell in the virtual column
    // is needed. Get the value from the dictionary if the key exists.

    if (e.ColumnIndex == 0)
    {
        int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
        if (checkState.ContainsKey(orderID))
            e.Value = checkState[orderID];
        else
            e.Value = false;
    }

}

private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
    // Handle the notification that the value for a cell in the virtual column
    // needs to be pushed back to the dictionary.

    if (e.ColumnIndex == 0)
    {
        // Get the orderID from the OrderID column.
        int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;

        // Add or update the checked value to the dictionary depending on if the 
        // key (orderID) already exists.
        if (!checkState.ContainsKey(orderID))
        {
            checkState.Add(orderID, (bool)e.Value);
        }
        else
            checkState[orderID] = (bool)e.Value;
    }
}

      

+4


a source


I'm surprised this is happening, but if there is no other way around it in the worst case, you can set the sort to be programmatic and then handle when the user clicks on the column header, keep a list of those items that are checked, do the sort programmatically, and then check all items to be checked.



+1


a source







All Articles