Draw diagonal lines in cell of DataGrid

Posted on 2006-06-06
Last Modified: 2008-02-01
Instead of graying out datagrid cells with a gray color, I would like to gray them out by drawing diagonal lines in the cell. How can I do this? Any ideas.

This is a windows forms application

Question by:Sanmarie
    LVL 96

    Expert Comment

    by:Bob Learned
    .NET version?


    Author Comment


    .NET Version 1.1. Visual Studio 2003
    LVL 96

    Expert Comment

    by:Bob Learned
    1) You need to define your own class, inheriting from DataGridTextBoxColumn, and handle the paint yourself.

    2) Here is an example of what I mean:

    Public Class DataGridColorTextBoxColumn
      Inherits DataGridTextBoxColumn

      Private m_maxColumn As DataGridColorTextBoxColumn
      Public Property MaxValueColumn() As DataGridColorTextBoxColumn
          Return m_maxColumn
        End Get
        Set(ByVal Value As DataGridColorTextBoxColumn)
          m_maxColumn = Value
        End Set
      End Property

      Private m_source As CurrencyManager

      Private Function GetRowValue(ByVal rowNumber As Integer) As String
        Return Me.GetColumnValueAtRow(m_source, rowNumber)
      End Function

      Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
       ByVal bounds As Rectangle, ByVal source As CurrencyManager, _
       ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)


          If Me.MaxValueColumn Is Nothing Then
            Throw New ArgumentException("Max value column is not set.")
          End If

          m_source = source

          Dim columnValue As Double = Me.GetRowValue(rowNum)

          If columnValue > Me.MaxValueColumn.GetRowValue(rowNum) Then
            backBrush = New SolidBrush(Color.Green)
            foreBrush = New SolidBrush(Color.White)
          End If

        Catch ex As Exception



          MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

        End Try

      End Sub

    End Class


    Author Comment

    Thank you for the info.

    I have already inherited from a datagridTextBoxColumn and I can set the color of any cell I want by overriding the paint method. But, instead of coloring a cell with a color by using
    backbrush = new solidbrush(Color.Green)

    I want to "color" the cell with diagonal lines running across it. How do I implement the diagonal lines? Do I have to use a pen object or something like that?


    LVL 96

    Accepted Solution

    Since you have the Graphics instance, you could do something like this:

    g.DrawLine(Pens.DarkGray, 20, 20, 100, 100)


    Author Comment


    I guess to get many diagonal lines, I could put this statement in a for loop, looping for less than the height of the cell. I will try it and let you know.


    Author Comment


    It works. Thanks a lot


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    Get rid of selecting hard-coded cell 8 53
    Update a DataTable to the database. 3 26
    Need help with a query 3 35
    GridDataView 6 15
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now