• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

Conditional Formatting Of Windows Form DataGrid Rows

Hi.,
I want to change the color of datagrid row at runtime. I have a Windows form (vb.net). On that form i have 3 datagrid some binded to dataset and some to arraylist. I have applied the table style's also. I want to change the one of my datagrid  row color on some data condition, and this formating should apply for a particular datagrid and other should not be distrub. I receive the new data in dataset and arraylist every one miniute. I am showing new data in datagrid (append rows). So how can i change the color of the datagrid and when the new row is added to datagrid i sort them in desc so i can get the new values first but when the client area of datagrid is filled means when scroll bar of datagrid get visible then the selected index is not set to the first row.. Its still on the old row how can i slove these problem.

Regarding conditional formatting i have seen meany example but i was not able to apply them for a single datagrid, when i applied it all the datagrid color changes ie all 3 datagrid.

Thanks in advance
Regards
0
skullden
Asked:
skullden
1 Solution
 
arif_eqbalCommented:
I hope you are using DataGridTableStyle to format your DataGrid now and you must be using DatagridTextBoxColumn object to format it.

What you do is use DataGridFormattableTextBoxColumn and Handle its event so that at runtime you can decide the row/cell color based on your text

Use

Dim FormatCell as DataGridFormattableTextBoxColumn
FormatCell = New DataGridFormattableTextBoxColumn(0) 'Column No

'Rest of the things As You do
FormatCell.MappingName = DataTable1.Columns(0).ColumnName
FormatCell.HeaderText = DataTable1.Columns(0).ColumnName

'Then
AddHandler FormatCell.SetCellFormat, AddressOf CellFormatHandler


Now the CellFormatHandler Event

    Private Sub CellFormatHandler (ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
         'I THINK THIS IS WHAT YOU NEED, CHECK IF THE ROW BEING PAINTED HAS THE REQUIRED TEXT AT COLUMN 0, IF YES COLOR THE ROW AS RED
         'Here e.Row contains the Row Number which is being Painted so
         If CType(dataGrid1.DataSource, DataTable).Rows(e.Row).Item(0) = "MyValue" Then
            e.BackBrush = Brushes.Red
        End If
End Sub


0
 
skulldenAuthor Commented:
Hi.,
Thanks for the quick response.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now