How to format individual cells or rows of a VB.NET DataGrid?

Discussion in 'Microsoft Dot NET' started by Guest, Jun 30, 2005.

  1. Guest

    Guest Guest

    I know that you can format a grid and its columns using DataGridTableStyle
    and DataGridTextBoxColumn in VB.NET. But can you change the format of the
    individual rows, or even cells, to something other than the default?

    For instance, I have a datagrid displaying a list of products in stock. I
    want to highlight the products in red if the product is out of stock. How
    should I implement this?

    Please let me know if you have the answer. Thanks.
     
    Guest, Jun 30, 2005
    #1
    1. Advertisements

  2. Guest

    Mona Guest

    Hi Richard,

    You can do this by inheriting DataGridTextBoxColumn and overriding the Paint
    method to conditionally
    change the cell format. Here is an example that colors cells based on their
    value:

    Public Class DataGridColoredTextBoxColumn
    Inherits DataGridTextBoxColumn
    Public Sub New()
    End Sub
    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)

    Try
    Dim o As Object
    o = Me.GetColumnValueAtRow(source, rowNum)
    If (Not (o) Is Nothing) Then
    Dim c As Char
    c = CType(o, String).Substring(0, 1)
    If (c = "Test") Then

    backBrush = new SolidBrush(Color.Red);
    foreBrush= new SolidBrush(Color.White);
    End If
    End If
    Catch ex As Exception
    ' empty catch
    Finally
    MyBase.Paint(g, bounds, source, rowNum, backBrush,
    foreBrush, alignToRight)
    End Try
    End Sub
    End Class

    Hope this helps.
    Thanks
    Mona[Grapecity]



    "Richard" <> wrote in message
    news:...
    >I know that you can format a grid and its columns using DataGridTableStyle
    > and DataGridTextBoxColumn in VB.NET. But can you change the format of the
    > individual rows, or even cells, to something other than the default?
    >
    > For instance, I have a datagrid displaying a list of products in stock. I
    > want to highlight the products in red if the product is out of stock. How
    > should I implement this?
    >
    > Please let me know if you have the answer. Thanks.
     
    Mona, Jul 1, 2005
    #2
  3. Guest

    Guest Guest

    Thanks for the help. It's working great.

    My next question is: How do I change the format of other cells on the same
    row?

    As in your example, if the first cell of current row contains "Test", the
    background color of the first cell wil be changed to RED, and the font color
    is changed to WHITE. How do I propagate the same format to the rest cells
    basing on the value of the FIRST cell?

    Richard


    "Mona" wrote:

    > Hi Richard,
    >
    > You can do this by inheriting DataGridTextBoxColumn and overriding the Paint
    > method to conditionally
    > change the cell format. Here is an example that colors cells based on their
    > value:
    >
    > Public Class DataGridColoredTextBoxColumn
    > Inherits DataGridTextBoxColumn
    > Public Sub New()
    > End Sub
    > 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)
    >
    > Try
    > Dim o As Object
    > o = Me.GetColumnValueAtRow(source, rowNum)
    > If (Not (o) Is Nothing) Then
    > Dim c As Char
    > c = CType(o, String).Substring(0, 1)
    > If (c = "Test") Then
    >
    > backBrush = new SolidBrush(Color.Red);
    > foreBrush= new SolidBrush(Color.White);
    > End If
    > End If
    > Catch ex As Exception
    > ' empty catch
    > Finally
    > MyBase.Paint(g, bounds, source, rowNum, backBrush,
    > foreBrush, alignToRight)
    > End Try
    > End Sub
    > End Class
    >
    > Hope this helps.
    > Thanks
    > Mona[Grapecity]
    >
    >
    >
    > "Richard" <> wrote in message
    > news:...
    > >I know that you can format a grid and its columns using DataGridTableStyle
    > > and DataGridTextBoxColumn in VB.NET. But can you change the format of the
    > > individual rows, or even cells, to something other than the default?
    > >
    > > For instance, I have a datagrid displaying a list of products in stock. I
    > > want to highlight the products in red if the product is out of stock. How
    > > should I implement this?
    > >
    > > Please let me know if you have the answer. Thanks.

    >
    >
    >
     
    Guest, Jul 5, 2005
    #3

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mario Vargas

    Getting all the datagrid rows

    Mario Vargas, Jul 1, 2003, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    1,937
    Chuck Grimsby
    Jul 2, 2003
  2. Shivang
    Replies:
    0
    Views:
    243
    Shivang
    Sep 10, 2003
  3. Andrew Boothman

    VB.NET, VC.NET & C#.NET and the .NET framework

    Andrew Boothman, Jul 23, 2004, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    316
    David Williams , VB.NET MVP
    Jul 23, 2004
  4. Bob Rosen
    Replies:
    0
    Views:
    148
    Bob Rosen
    Dec 20, 2004
  5. ali asjad
    Replies:
    1
    Views:
    224
    Chris, Master of All Things Insignificant
    Feb 25, 2005
Loading...

Share This Page