473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 3562
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.