Search code examples
c#winformscheckboxobjectlistview

How to check all the items in objectlistview c#?


I am using Objectlistview in my WFA to create a chekedlistbox. I want to have a button called "Select all" that the user can click on it and all the rows are selected just by one click. I have been using the following code which works and all the checkboxes will be selected

 private void btnSelectallModule_Click(object sender, EventArgs e)
    {
        
        foreach (ListViewItem item in dataListView1.Items)
        {
            item.Checked = true;
        }

    }

The problem is that when I check all the items using this button then I hover over each item it will be unchecked automatically without even clicking on that item, which is so weird because I did not intend to do that in the code. Does anyone know what is going on and how can I fix this? Thanks


Solution

  • In general do NOT manipulate the ListViewItem objects when working with ObjectListView.

    There is a Method dataListView1.CheckAll() that will do exactly what you are trying to do - check all items. Using that method will properly set the internal check states of the OLV control and prevent them from getting visually unchecked when the view refreshes (when hovering the mouse over items).