Click here to Skip to main content
15,881,807 members
Articles / Programming Languages / Visual Basic
Article

Realy Nice DataGridView

Rate me:
Please Sign up or sign in to vote.
1.42/5 (10 votes)
17 Feb 2008CPOL 88.1K   1.4K   70   11
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.

VB
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.
VB
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)


Written By
Technical Lead Interglobe Technologies
India India
Hello, I am Deepak Bhardwaj. I am working as Module Lead at Interglobe Technologies (IGT) in Microsoft .NET Technologies. I have worked on Winforms and WPF.

Comments and Discussions

 
QuestionNice post Pin
Tridip Bhattacharjee12-Dec-13 21:56
professionalTridip Bhattacharjee12-Dec-13 21:56 
Questionappearance of gradient in control DataGridView Pin
Emanuel vargas20-May-12 10:36
Emanuel vargas20-May-12 10:36 
GeneralProblems with GridLines Pin
Member 463059812-May-10 16:22
Member 463059812-May-10 16:22 
QuestionHow to improve performance Pin
Member 463059812-May-10 16:17
Member 463059812-May-10 16:17 
Generale.State = 97 Pin
nosuch778-Mar-10 3:48
professionalnosuch778-Mar-10 3:48 
GeneralRe: e.State = 97 Pin
Member 740216625-Jun-11 3:36
Member 740216625-Jun-11 3:36 
GeneralMy vote of 2 Pin
ademoo2-Feb-09 1:51
ademoo2-Feb-09 1:51 
GeneralBug with empty table :-| Pin
Michał Zalewski6-Jul-08 11:21
Michał Zalewski6-Jul-08 11:21 
GeneralError opening form... Pin
karenpayne17-Apr-08 3:39
karenpayne17-Apr-08 3:39 
GeneralHide Cell upper and left line when user edit cell Pin
Goje063-Apr-08 20:52
Goje063-Apr-08 20:52 
QuestionHow about a screenshot or two? Pin
David Catriel17-Feb-08 2:26
David Catriel17-Feb-08 2:26 

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.