8

Well i have a windows forms application in which i add a couple of listViews in order to hold some data for the user and it looks like this

enter image description here

As you see my form backcolor is black so the list view's grid lines and header white color makes an annoying contrast so after an hour searching without a luck i decided to ask here.

[Question] : How could i edit the colors of the Header & Grid Lines of the list view to match my needs ?

Daniel Eugen
  • 2,712
  • 8
  • 33
  • 56
  • 1
    This maybe? http://stackoverflow.com/questions/1814692/change-the-background-color-of-winform-listview-headers – RenniePet Aug 17 '13 at 00:50
  • Or this? http://stackoverflow.com/questions/6008226/are-there-any-good-and-free-devexpress-data-grid-alternatives-for-winforms – RenniePet Aug 17 '13 at 00:54
  • 1
    @RenniePet : excuse me but can you see that both of the topics you provided are far from my question ?. one for decexpress and the other only solves the problem of the Header color, what about those grid lines ? – Daniel Eugen Aug 17 '13 at 00:59
  • also i can't change to anything else because whole of my code is built on listview so i need a fix for ListView no alternatives – Daniel Eugen Aug 17 '13 at 01:00
  • is there is no answer for my question ? – Daniel Eugen Aug 17 '13 at 02:14

2 Answers2

13

It looks like that there is not anyone who's interested in customizing a ListView to support Grid Line Color. I've tried this one and want to share here. It's not really good by a little flicker (not really much) when you scroll the ListView items. However it's acceptable. I think I lack some knowledge of win32 here to make it more perfect:

public class CustomListView : ListView {
        bool scrollDown;
        int lastScroll;
        public Color GridLinesColor {get;set;}
        [DllImport("user32")]
        private static extern int GetScrollPos(IntPtr hwnd, int nBar);
        public CustomListView(){
           GridLinesColor = Color.Red;
           DoubleBuffered = true;
           base.GridLines = false;//We should prevent the default drawing of gridlines.
        }
        public new bool GridLines {get;set;}
        protected override void WndProc(ref Message m)
        {                
            if (m.Msg == 0x20a){//WM_MOUSEWHEEL = 0x20a
                scrollDown = (m.WParam.ToInt64() >> 16) < 0;
            }
            if (m.Msg == 0x115){//WM_VSCROLL = 0x115
                int n = (m.WParam.ToInt32() >> 16);
                scrollDown = n > lastScroll;
                lastScroll = n;
            }                
            base.WndProc(ref m);
            if (m.Msg == 0xf && GridLines && Items.Count > 0&&View==View.Details)//WM_PAINT = 0xf
            {                    
                using (Graphics g = CreateGraphics())
                {
                    using(Pen p = new Pen(GridLinesColor)){
                      int w = -GetScrollPos(Handle, 0);
                      for (int i = 0; i < Columns.Count; i++)
                      {
                        w += Columns[i].Width;
                        g.DrawLine(p, new Point(w, 0), new Point(w, ClientSize.Height));
                      }
                      int a = Items[0].Bounds.Bottom - 1;
                      int b = Height - Items[0].Bounds.Y;
                      int c = Items[0].Bounds.Height;
                      for (int i = scrollDown ? a + (b/c) * c : a ; scrollDown ? i >= a : i < b ; i += scrollDown ? -c : c)
                      {
                        g.DrawLine(p, new Point(0, i), new Point(ClientSize.Width, i));
                      }                                      
                    }          
                }                 
            }

        }
}

UPDATE: Thanks to suggestion of Cody Gray, I added code to handle horizontal scrolling. I use GetScrollPos for simplicity because as recommended by MSDN documentation page, we should use GetScrollInfo instead.

enter image description here

King King
  • 61,710
  • 16
  • 105
  • 130
  • 2
    +1 for actually attempting a solution to the question being asked, even after a useless answer was already accepted. I can't get this to flicker on my WinForms VM (Win 7, with and without Aero). But there are some other big problems with the code. One of them is horizontal scrolling—that isn't implemented and the resulting effect is ugly. Also, you're leaking Pen objects by failing to call Dispose. By wrapping the creation in a using statement, you could even re-use the same pen object *and* have it automatically disposed. And I don't understand why you need to track the `scrollDown` state. – Cody Gray - on strike Aug 17 '13 at 07:30
  • @CodyGray thank you for your suggestion, in fact this is just demo. the `scrollDown` is required so that we know if user scrolls down, we'll draw the `Lines` from `Bottom` to `Top`, otherwise (scroll up) we'll draw the `Lines` from `Top` to `Bottom`. It reduces a little flicker (in fact that's not flicker, it's caused by the drawing of default `white lines`.) – King King Aug 17 '13 at 07:39
  • 1
    I may change the accepted answer to be the best answer provided there is no problem at that, However i will do that if the code is full so that anyone can benefit from my thread in the future. – Daniel Eugen Aug 17 '13 at 16:01
  • +1 despite the crazy flickering. IMO, a better approach would be turning it to an owner-drawn and draw the grid lines in **WM_DRAWITEM** which has no flickering at all. Of course you'll need to draw the items text too. – RCECoder Jul 24 '23 at 21:28
-1

You can perform that in a DataGrid, but I don't think there's an easy way for ListView, since there are no properties for those lines, unlike DataGrid.

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="HorizontalGridLinesBrush" Value="Red"/>
    <Setter Property="VerticalGridLinesBrush" Value="Green"/>
</Style>

Put it into application resources or window resources.

Other than that, there's a way to change the border color of each ListViewItem:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="BorderBrush" Value="Red"/>
</Style>
B.K.
  • 9,982
  • 10
  • 73
  • 105
  • The gridlines property is only to set whether they are visible or not... However it does not provide any color adjustment options which i need the most. – Daniel Eugen Aug 17 '13 at 02:39
  • @Nobbacode The rest of things are good however it does not solve my biggest problem which is the color of the grid lines – Daniel Eugen Aug 17 '13 at 02:40
  • @DanialEugen You can't do it with the default class unless you override some things (refer to this): http://www.hightechtalks.com/dotnet-framework-winforms-controls/color-grid-listview-539463.html (2nd post). You may also consider GridView: http://stackoverflow.com/questions/461404/c-sharp-how-do-i-change-the-gridlines-color-in-windows-forms-listview – B.K. Aug 17 '13 at 02:48