Altering Attributes in DataGrid

partham

Active member
Joined
Dec 27, 2005
Messages
41
Location
Kolkata, India
Programming Experience
10+
Dear Sir,

I am using a Datagrid in a VB.Net application (using Microsoft Visual Studio .Net 2003). I would like to know

1. if it is possible to hide a specific row in the Datagrid. If so, how?
2. if it is possible to display the value in a specific cell of the DataGrid in a different Colour. If so, how?

I have used Table and Column Styles for displaying the data in the Datagrid.

Thanks in advance.

Regards,
Partha.:(
 
1. You cannot hide a row in VS2003.
2. You can do this by derivation from DataGridTextBoxColumn and by overriding the Paint method to conditionally set the backColor and foreColor. Sample code is –

VB.NET:
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)
' the idea is to conditionally set the foreBrush and/or backbrush 
' depending upon some crireria on the cell value 
' Here, we color anything that begins with a letter higher than 'F' 
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 > "F") Then
' could be as simple as 
' backBrush = new SolidBrush(Color.Pink); 
' or something fancier... 
backBrush = New LinearGradientBrush(bounds, _ 
Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20),_ 
LinearGradientMode.BackwardDiagonal)
foreBrush = New SolidBrush(Color.White)
End If
End If
Catch ex As Exception
' empty catch 
Finally
' make sure the base class gets called to do the drawing with 
' the possibly changed brushes 
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, _ 
alignToRight)
End Try
End Sub
End Class
 
Last edited by a moderator:
Back
Top