PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?Utf-8?B?UmljaGFyZA==?=
Guest
Posts: n/a
 
      30th Jun 2005
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.
 
Reply With Quote
 
 
 
 
Mona
Guest
Posts: n/a
 
      1st Jul 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.



 
Reply With Quote
 
 
 
 
=?Utf-8?B?UmljaGFyZA==?=
Guest
Posts: n/a
 
      5th Jul 2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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.

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FONT / ROW ACCESS 2007 Unable to change font size or colour of individual text or heigth of individual rows without all text & rows changing moto Microsoft Access 1 12th Nov 2008 08:23 PM
Can I have individual folder for each individual e-mail account? =?Utf-8?B?VA==?= Microsoft Outlook Installation 1 30th Dec 2005 11:52 AM
Deliver mail from individual addresses to individual pst files =?Utf-8?B?Sm9zZWZrMTAw?= Microsoft Outlook Discussion 3 14th Oct 2005 04:17 PM
[OT] [Individual] News.Individual.NET not free of charge anymore Diego Windows XP General 1 19th Feb 2005 01:52 AM
Format entire columns/rows or individual cells Shatin Microsoft Excel Programming 1 12th Mar 2004 10:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:58 AM.