lantonis
lantonis

Reputation: 143

change row colors in a specific OutlookGroupBy group in UltraGrid in VB.net

I am using an ultragrid and inside i have values that i load into a datatable from a database. In my grid i group the rows into groups with OutlookGroupBy code. My rows in the grid are in two categories. The Priority 1 and 0. I want when the data are loaded the rows that are in priority 1 to get color red, and the others in priority 0 to be in normal color.

I use the ultragrid pro grammatically so i didnt use any of its features in the editor.

here is how i initialize my grid and how i load from database from another class:

 Dim dt As DataTable = Nothing

        Timer1.Enabled = True
        UltraGrid1.DataSource = Nothing
        Generic.openOrders(dt)
        UltraGrid1.DataSource = dt

        Dim band As Infragistics.Win.UltraWinGrid.UltraGridBand = UltraGrid1.DisplayLayout.Bands(0)
        UltraGrid1.DisplayLayout.ViewStyleBand = Infragistics.Win.UltraWinGrid.ViewStyleBand.OutlookGroupBy
        band.SortedColumns.Add(band.Columns("PRIORITY"), True, True)
        band.SortedColumns.Add(band.Columns("ORDERID"), False, True)
        band.SortedColumns.Add(band.Columns("ORDERTIME"), False, True)

anyone has any idea of how can i change the row color into the priority 1 subrows??

Upvotes: 0

Views: 2092

Answers (1)

Steve
Steve

Reputation: 216313

You can try with this approach:

First you need to subscribe the event InitializeRow, so add this by the designer or by code (eg. in Form_Load or before setting the DataSource of the grid)

grd.InitializeRow += new InitializeRowEventHandler(grd_InitializeRow);

then, in the event, write code like this

private void grd_InitializeRow(object sender, InitializeRowEventArgs e)
{
    if(e.Row.Band.Index == 1)
    {
         if(Convert.ToInt32(e.Row.Cells["PRIORITY"].Value) == 1)
              e.Row.Appearance.BackColor = Color.LightGreen;
    }
}

Keep in mind that if you have set CellAppearance they will have precedence on RowAppearance and, if you have many rows it is better to initialize an Appearance object, store it in the grid.DisplayLayout.Appearances collection and reuse the same object for every row involved.

Moreover, always with the aim of improving the performance, to get the cell value it is better to use the GetCellValue method of the row. This will avoid the creation of the a full Cell object just to retrieve its value. Things are a bit more complicated because you need an UltraGridColumn and not just the name of the column, but in the InitializeRow event, fired for each row, this is little price to pay.

private void grd_InitializeRow(object sender, InitializeRowEventArgs e)
{
    if(e.Row.Band.Index == 1)
    {
         UltraGridColumn priorityColumn = e.Row.Band.Columns["PRIORITY"];
         if(Convert.ToInt32(e.Row.GetCellValue(priorityColumn)) == 1)
              e.Row.Appearance.BackColor = Color.LightGreen;
    }
}

EDIT: The same code in VB.NET

....
AddHandler grd.InitializeRow, AddressOf Me.grd_InitializeRow
....


Private Sub grd_InitializeRow(sender As System.Object, e As InitializeRowEventArgs)
    If e.Row.Band.Index = 1 Then
        Dim priorityCol As UltraGridColumn = e.Row.Band.Columns("PRIORITY")
        If Convert.ToInt32(e.Row.GetCellValue(priorityCol)) = 1 Then
            e.Row.Appearance.BackColor = Color.LightGreen
        End If
    End If
End Sub

Also, the use the UltraGridColumn class, you need to add at the start of file

Imports Infragistics.Win.UltraWinGrid

Upvotes: 2

Related Questions