locked
How to change the color of a the gridlines in a listview in a c# winform RRS feed

  • Question

  • I wanted to know how I can change the color and the width of the gridlines in a listview. Also, I googled this question and a lot of the answers I got were to change to a datagridview. I really still want to use a listview, but I also wanted to know the difference in fast performance time between a datagridview and a listview.

    Debra has a question

    Thursday, August 7, 2014 4:52 PM

Answers

  • Not all ListViews, but you can write your own class using the standard ListView as a base, then use that new class wherever you want the new behavior.

    public class MyListView : ListView
    {
    
       // Subscribe to the events in the constructor.
       public MyListView()
       {
             this.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.MyDrawColumnHeader);
             this.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(this.MyDrawSubItem);
       }
    
    
        private void MyDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // Your speciaised drawing code goes in here.
        }
    
        private void MyDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
           // And here (probably just exactly the same as in the previous post).
        }
    
    }

    Then use MyListView (or whatever you choose to call it) instead of ListView.

    • Marked as answer by Barry Wang Friday, August 15, 2014 6:52 AM
    Friday, August 8, 2014 11:34 PM

All replies

  • Greetings Debra.

    Set OwnerDraw to true, GridLines to false, and subscribe to the DrawSubItem and DrawColumnHeader events.

    private void listViewNew_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height); e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rect); // Draw the grid in red.

    // Draw the text. This is necessary because OwnerDraw means that it won't be drawn automatically for you. StringFormat format = new StringFormat(); format.Trimming = StringTrimming.EllipsisCharacter; e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(listViewNew.ForeColor), e.Bounds, format); e.DrawDefault = false; } // Without subscribing to this event, the column heading go missing for some unknown reason. private void listViewNew_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; }


    I don't know why the DrawColumnHeader bit is necessary. It looks like a bug in VS to me

    Thursday, August 7, 2014 11:51 PM
  • Greetings again.

    In my last post I neglected code needed to highlight the selected item in the list. I'm not 100% certain about which colours are applicable, but I think the DrawSubItem code should look something like this...

          private void listViewNew_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
          {
             // Draw the background in an appropriate colour for selected/not selected, and set the corresponding text colour.
             Color colourText;
             if (e.Item.Selected)
             {
                e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), e.Bounds);
                colourText = SystemColors.HighlightText;
             }
             else
             {
                e.Graphics.FillRectangle(new SolidBrush(e.SubItem.BackColor), e.Bounds);
                colourText = e.SubItem.ForeColor;
             } 
             
             Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
             e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rect);
    
             StringFormat format = new StringFormat();
             format.Trimming = StringTrimming.EllipsisCharacter;
             e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(colourText), e.Bounds, format);
             e.DrawDefault = false;
          }
    

    Friday, August 8, 2014 12:42 AM
  • Is there a way to make it into an extended method for all listviews?

    Debra has a question

    Friday, August 8, 2014 2:22 PM
  • Not all ListViews, but you can write your own class using the standard ListView as a base, then use that new class wherever you want the new behavior.

    public class MyListView : ListView
    {
    
       // Subscribe to the events in the constructor.
       public MyListView()
       {
             this.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.MyDrawColumnHeader);
             this.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(this.MyDrawSubItem);
       }
    
    
        private void MyDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // Your speciaised drawing code goes in here.
        }
    
        private void MyDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
           // And here (probably just exactly the same as in the previous post).
        }
    
    }

    Then use MyListView (or whatever you choose to call it) instead of ListView.

    • Marked as answer by Barry Wang Friday, August 15, 2014 6:52 AM
    Friday, August 8, 2014 11:34 PM