Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Adding Checkbox to a List View Column Header in C# WindowsForm Application

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
7 Feb 2018CPOL 41.6K   1.9K   13   4
Adding Checkbox to a List View Column Header in C# WindowsForm application

Introduction

Adding checkbox in listview column header, initially Listview doesn't contain checkbox in a column header. We need to create it by using OwnerDraw and some sub items.

Background

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.

Using the Code

  • 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.

The code snippet is as given below:

C#
//
  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();
//

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.

Special thanks to Reza Aghaei for his help!

License

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


Written By
Software Developer Shifttocloud Software India PVT Ltd
India India
Hello Techies!

This is SANTOSH KOKATNUR.

From I N D I A

Well, I am working in Shifttocloud Software India PVT LTD as a Sr.Software Engineer(c# .NET Dev) in Bangalore, India.

I am a hands-on Software Developer, I have developed console, database, and Desktop applications using current versions of Microsoft Visual Studio (Frameworks: 1.1, 2.0, 3.5, 4.0, 4.5).

As a highly motivated computer professional with the enthusiasm to accept new challenge, With a proven record of achievement in positions of similar scope and responsibility.

Thanks!

Comments and Discussions

 
PraiseWell Done! Pin
Alan Burkhart24-Mar-18 14:36
Alan Burkhart24-Mar-18 14:36 
GeneralRe: Well Done! Pin
Santosh Kokatnur5-Jun-18 1:22
Santosh Kokatnur5-Jun-18 1:22 
PraiseGood to know about this stuff. Pin
Member 1296751825-Jan-17 3:26
Member 1296751825-Jan-17 3:26 
GeneralRe: Good to know about this stuff. Pin
Santosh Kokatnur28-May-17 19:12
Santosh Kokatnur28-May-17 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.