Articles in this section
Category / Section

How to apply alternate backcolor for the rows in both parent and child grid in WinForms DataGrid (SfDataGrid)?

1 min read

Apply backcolor to rows in parent and child grid

You can apply alternate back color to rows in both the parent and child grids by hooking the QueryRowStyle event for both the child and parent DataGrid.

C#

this.sfDataGrid1.QueryRowStyle += ParentDataGrid_QueryRowStyle;
void ParentDataGrid_QueryRowStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs e)
{
    if (sfDataGrid1.DetailsViewDefinitions.Count > 0)
    {
        if (e.RowType == Syncfusion.WinForms.DataGrid.Enums.RowType.DefaultRow)
        {
            if ((e.RowIndex / (sfDataGrid1.DetailsViewDefinitions.Count + 1)) % 2 == 0)
                e.Style.BackColor = Color.LightGreen;
            else
                e.Style.BackColor = Color.Yellow;
        }
    }
}
gridViewDefinition.DataGrid.QueryRowStyle += ChildDataGrid_QueryRowStyle;
void ChildDataGrid_QueryRowStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs e)
{
    if(e.RowType == Syncfusion.WinForms.DataGrid.Enums.RowType.DefaultRow)
    {
        if (e.RowIndex % 2 == 0)
            e.Style.BackColor = Color.Yellow;
        else
            e.Style.BackColor = Color.LightGreen;
    }
}

VB

AddHandler Me.sfDataGrid1.QueryRowStyle, AddressOf ParentDataGrid_QueryRowStyle
Private Sub ParentDataGrid_QueryRowStyle(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs)
     If sfDataGrid1.DetailsViewDefinitions.Count > 0 Then
       If e.RowType = Syncfusion.WinForms.DataGrid.Enums.RowType.DefaultRow Then
          If (e.RowIndex / (sfDataGrid1.DetailsViewDefinitions.Count + 1)) Mod 2 = 0 Then
                e.Style.BackColor = Color.LightGreen
          Else
             e.Style.BackColor = Color.Yellow
          End If
       End If
     End If
End Sub
AddHandler gridViewDefinition.DataGrid.QueryRowStyle, AddressOf ChildDataGrid_QueryRowStyle
Private Sub ChildDataGrid_QueryRowStyle(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs)
     If e.RowType = Syncfusion.WinForms.DataGrid.Enums.RowType.DefaultRow Then
       If e.RowIndex Mod 2 = 0 Then
          e.Style.BackColor = Color.Yellow
       Else
          e.Style.BackColor = Color.LightGreen
       End If
     End If
End Sub

 

Show the backcolor is applied in parent and child grid rows

sample: How to apply alternate back color to both the parent and child grids.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied