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

Thanks for the help. It's working great.

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

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?


"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" <Richard@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:82420575-F20B-45D0-8D4A-641E1CBD87B8@xxxxxxxxxxxxxxxx
> >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.

Relevant Pages

  • Explanation - Union doesnt work with format mergecells...
    ... I want to ensure the formats after someone make cut and past, drag and drop ... If so, the format vanishes. ... Since there is a lot of different cells, ... .ReadingOrder = xlContext ...
  • RE: Mail Merge
    ... applied in the worksheet cells. ... exactly the same in your merged documents as they look in the worksheet cells. ... Muck around with field codes in Word ... Format currency and other numbers by using field codes ...
  • Re: Cell resizing
    ... If the rows have been formatted to Autofit you will not see any indication other ... "Format Rows" the rows should expand with no error ... Dim cWdth As Single, MrgeWdth As Single ... Make sure cells are set for wrap text and rows to autofit. ...
  • Re: Using DDE as source in mail merge
    ... So an autocorrect will cause the 20% to multiply ... or a single cell has a weird format set. ... If your worksheet is formatted using numbers and the cells formatted as ... longer to add the switches - and you only have to do it once. ...
  • Re: performing calculations on time data
    ... the worksheet and not in vba code. ... the all cells are formatted as general. ... This little test worked for me in Excel 2003... ... When the format was adjusted ...