3

enter image description here

I want to add checkbox in header and also if I checked in header checkbox it should be like select all and deselect All... just like above image!

enter image description here

I want to create this type of listview header with checkbox in c#

Community
  • 1
  • 1
Santosh Kokatnur
  • 357
  • 3
  • 9
  • 19
  • 1
    have you seen this [question](http://stackoverflow.com/questions/5554068/listview-checkbox-selecting-all-deslecting-all-c-sharp-4-0?answertab=votes#tab-top) ? – Yog Mar 04 '16 at 06:47
  • @Robotnik look at the above image i want to add checkbox to listview header – Santosh Kokatnur Mar 04 '16 at 06:57
  • 1
    You can set `OwnerDraw` property of `ListView` to true and draw a `ChceckBox` on first column header and handle `ColumnClick` to perform select/deselect all. – Reza Aghaei Mar 04 '16 at 06:59

1 Answers1

16

You can set OwnerDraw property of ListView to true and draw a ChceckBox on first column header and handle ColumnClick to perform select/deselect all.

  • For drawing the ListView you need to handle DrawColumnHeader, DrawItem and DrawSubItem events.

  • Draw checkbox in DrawColumnHeader event.

  • Set e.DrawDefault = true; for other drawing events to draw default rendering.
  • Handle ColumnClick event and store the checked state of column header in tag of column. Also for each item of list view, set Checked property to perform select/deselect all.

Code:

private void listView1_DrawColumnHeader(object sender, 
                                        DrawListViewColumnHeaderEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        e.DrawBackground();
        bool value = false;
        try
        {
            value = Convert.ToBoolean(e.Header.Tag);
        }
        catch (Exception)
        {
        }
        CheckBoxRenderer.DrawCheckBox(e.Graphics, 
            new Point(e.Bounds.Left + 4, e.Bounds.Top + 4),
            value ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal :
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
    }
    else
    {
        e.DrawDefault = true;
    }
}

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    e.DrawDefault = true;
}

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    e.DrawDefault = true;
}

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    if (e.Column == 0)
    {
        bool value = false;
        try
        {
            value = Convert.ToBoolean(this.listView1.Columns[e.Column].Tag);
        }
        catch (Exception)
        {
        }
        this.listView1.Columns[e.Column].Tag = !value;
        foreach (ListViewItem item in this.listView1.Items)
            item.Checked = !value;

        this.listView1.Invalidate();
    }
}

Screenshot:

enter image description here

  • In the above image I have 3 columns.
  • I set empty text for first column.
  • I Set CheckBoxes property of ListView to true.
  • I Set empty text for items and added 2 sub items for each item.
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 2
    @SantoshKokatnur you could have given some credit to Reza Aghaei in your codeproject article... https://www.codeproject.com/Tips/1083589/Adding-Checkbox-to-a-List-View-Column-Header-in-Cs – Dom84 Feb 06 '18 at 15:14
  • 1
    @Dom84 have given credit to Reza in codeproject. Have a look. – Santosh Kokatnur Feb 07 '18 at 11:23