473,508 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 22 '05 #1
2 3566
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" <Ri*****@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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.

Nov 22 '05 #2
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" <Ri*****@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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.


Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1542
by: Bob Rosen | last post by:
The description given in "Customizing Items Dynamically in the DataList or DataGrid Web Server Control" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbCode/...
2
838
by: Richard | last post by:
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...
1
2301
by: J.B | last post by:
I have a datagrid that will display different datasets, (it runs different sprocs based on a value in the querystring) but it will always return 6 columns. The 5th and 6th columns are always Date...
2
2672
by: Craig Buchanan | last post by:
Is there a way to add a row to a datagrid during the ItemDataBound event? I would like to add a 'header' row before the start of a group of rows. Thanks, Craig Buchanan
3
1154
by: NH | last post by:
Hi, Is it possible to format the background of rows (or cells) in a datagrid based on the value of the data in a particular cell. e.g. I have a datagrid of say 21 records, 3 records per day of...
6
1161
by: guffe | last post by:
I'm trying to display data in a datagrid. The rows are hours of the day and the coloums are days of the week. I need individual cell hight to display that a task takes more than one hour. How can it...
2
7305
by: jacqueharper | last post by:
I need to change the formatting of individual cells in a datagrid. I cannot figure out how to address individual cells by their column name. I have a DataTable which contains (for instance)...
2
984
by: Peter Afonin | last post by:
Hello, I have a datagrid with 17 columns - too many for one page. Is there a way to format the datagrid so the columns are located in two rows instead of one, or something like this? I would...
2
5137
by: Derek Vincent | last post by:
What must I do to overcome a problem with my dates becoming formatted as "2/22/2525 12:00:00 AM" in the datagrid? I want to handle all dates as short string of format "2/22/2525." Otherwise when I...
1
4732
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.