Conditional Formatting a Datagrid

rmcelroy

New member
Joined
Jan 23, 2007
Messages
2
Programming Experience
1-3
Hi,

Can someone please help. I have a simple datagrid populated with data from a query on an access table. I would like to highlight of certain rows depending on a value returned in the query. I have created a TableStyle and DataGridColumnStyles but can't figure out how to affect specific rows?

Thanks

Robert
 
I can share with you the component that I used before.

To use it, you have to add the table style:

VB.NET:
[SIZE=2]cs = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridFormattableTextBoxColumn(1)[/SIZE]
[SIZE=2]cs.MappingName = "Column1"[/SIZE]
[SIZE=2]cs.HeaderText = "Column 1"[/SIZE]
[SIZE=2]cs.Width = 80[/SIZE]
[SIZE=2]cs.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]cs.Format = "dd/MM/yyyy"[/SIZE]
[SIZE=2]cs.NullText = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty[/SIZE]
[SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] cs.SetCellFormat, [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FormatCellEventHandler([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] FormatGridCells)[/SIZE]
[SIZE=2]dgTableStyle.GridColumnStyles.Add(cs)[/SIZE]

And also it's event handler's method

VB.NET:
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FormatGridCells([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridFormatCellEventArgs)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dgList.Item(e.Row, 1) = "ColorIt" [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]e.BackBrush = Brushes.Red[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]e.BackBrush = Brushes.White[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/SIZE]

 

Attachments

  • Component.zip
    2.7 KB · Views: 25
Back
Top