Click here to Skip to main content
Licence CPOL
First Posted 17 Feb 2008
Views 51,439
Downloads 572
Bookmarked 65 times

Realy Nice DataGridView

By | 17 Feb 2008 | Article
Really Nice Look n' Feel in a DataGridView

NiceGridView.JPG

Introduction

The article demonstrates how to give .net DataGridView a nice look n' feel, by handling the DataGridView CellPaining Event.

Function to draw the column header.

Protected Sub DrawColumnHeader(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        Dim h As Integer = e.CellBounds.Height
        Dim w As Integer = e.CellBounds.Width
        Dim h1 As Integer = h * 0.4
        Dim ct As New OfficeColorTable
        Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, w, h1)
        Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, h1, w, h - h1 + 1)
        Dim lb1 As LinearGradientBrush = _
    New LinearGradientBrush(r1, ct.ColumnHeaderStartColor, ct.ColumnHeaderMidColor1, Drawing2D.LinearGradientMode.Vertical)
        Dim lb2 As LinearGradientBrush = _
    New LinearGradientBrush(r2, ct.ColumnHeaderMidColor2, ct.ColumnHeaderEndColor, Drawing2D.LinearGradientMode.Vertical)
        Dim p As Pen = New Pen(ct.GridColor, 1)
        Dim frmt As StringFormat = New StringFormat
        frmt.Alignment = StringAlignment.Center
        frmt.FormatFlags = StringFormatFlags.DisplayFormatControl
        frmt.LineAlignment = StringAlignment.Center
        With e.Graphics
            .FillRectangle(lb1, r1)
            .FillRectangle(lb2, r2)
            .DrawRectangle(p, e.CellBounds)
        End With
    End Sub

Function to draw DataGridView Cells.
Protected Sub DrawCell(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
        Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
        Dim ct As New OfficeColorTable
        Dim cellColor As Color
        Dim borderColor As Color
        Dim p As Pen
        If e.State = 97 Or e.State = 105 Then
            borderColor = ct.GridColor
            cellColor = ct.ActiveCellColor
            p = New Pen(borderColor, 1)
        Else
            borderColor = ct.GridColor
            p = New Pen(borderColor, 1)
            If e.State = 109 Then
                cellColor = ct.ReadonlyCellColor
            Else
                cellColor = ct.DefaultCellColor
            End If

        End If
        If e.ColumnIndex < 0 Then
            cellColor = ct.ColumnHeaderMidColor2
        End If
        With e.Graphics
            .FillRectangle(New SolidBrush(cellColor), e.CellBounds)
            Dim rnd As New Renderer
            If e.State = 97 Then
                rnd.Fill3DRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
            End If
            If e.ColumnIndex < 0 Then
                If e.State = 97 Then
                    rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
                Else
                    rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007GrayHover, e.Graphics)
                End If
            End If
            .DrawRectangle(p, e.CellBounds)
            .DrawRectangle(p, New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, Me.Height))
        End With

    End Sub

License

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

About the Author

Theepak Bhardwaj

Web Developer

India India

Member

Hello, i m Deepak Bhardwaj. Currently working as a software developer in Glotech Enterprises at Ghaziabad in .Net Technology

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionappearance of gradient in control DataGridView PinmemberJ. manuel vargas10:36 20 May '12  
GeneralProblems with GridLines PinmemberMember 463059816:22 12 May '10  
QuestionHow to improve performance PinmemberMember 463059816:17 12 May '10  
Generale.State = 97 Pinmembernosuch773:48 8 Mar '10  
GeneralRe: e.State = 97 PinmemberMember 74021663:36 25 Jun '11  
GeneralMy vote of 2 Pinmemberademoo1:51 2 Feb '09  
GeneralBug with empty table :-| PinmemberDrucik_11:21 6 Jul '08  
GeneralError opening form... PinmemberGlimmerMan3:39 17 Apr '08  
GeneralHide Cell upper and left line when user edit cell PinmemberGoje0620:52 3 Apr '08  
QuestionHow about a screenshot or two? PinmemberMuaddubby2:26 17 Feb '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Feb 2008
Article Copyright 2008 by Theepak Bhardwaj
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid