Maintain selection state (Multi selection) and check box state when sorting or new item added or item deleted in datagrid view virtualmode RRS feed

  • Question

  • We are using datagrid view in virtual mode to display contacts having check box column and 4 new columns.

    Unable to get previous selected and checked rows when sorting or new contact or contact delete to in memory table.

    My behaviour has to be When check box checked row has to select automatically and if unchecked row has to un select.

    I can use tab, arrow keys and space bar to check and select the rows.

    I am using c# win-forms.

    Friday, September 4, 2015 11:58 AM

All replies

  • So you pretty much just want to select only those rows have the checkbox checked:

                foreach (DataGridViewRow row in dgv.Rows)
                    DataGridViewCheckBoxCell cell = row.Cells[1] as DataGridViewCheckBoxCell;
                    row.Selected = (bool)cell.Value;

    (Obviously,  your checkbox might not be index 1 ).

    And you need to set it selected when the user changes the checkbox value, something like

            private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dgv.CurrentCell;
                dgv.Rows[e.RowIndex].Selected = (bool)cell.EditedFormattedValue;


    This sort of thing is rather easier in wpf, by the way, you could just bind selected and the checkbox value to the same property.

    Friday, September 4, 2015 12:45 PM
  • In Virtual Mode it is not working as expected.

    Mine is windows application and has to support all keyboard keys like Tab,Space bar,arrow keys.

    I can't bind selected and check box to single property due to some of the selected items may not be checked.

    Currently I am handling Cell Value Needed , Cell Value Pushed (when check box state

    changed), cell formatting events.

    I tried to handle to maintain selected rows in in memory object when selection changed event handler but every selection changed object is clearing and adding. This is causing slowness.

    • Edited by Subbu J9 Wednesday, September 9, 2015 12:51 PM
    Wednesday, September 9, 2015 12:47 PM
  • Hi,

    Could you please share some codes or a sample with us?

    We don't know how you implement the function of CellValueNeeded and CellValuePushed and some relative function.

    If you could share it with us, we could help you better.

    Thursday, September 10, 2015 9:31 AM