Articles in this section
Category / Section

How to apply Alternate Row Color in SfDataGrid?

1 min read

SfDataGrid allows you to set alternate row color by setting the SfDataGrid.GridStyle.AlternatingRowColor property. The default value of this property is Color.Default and hence applies the record background color for all rows in that case. If a value is set for this property then the grid appears with the specified alternate row color.

Refer the following code example which illustrates how to set AlternatingRowColor in SfDatGrid.

 
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        dataGrid.GridStyle.AlternatingRowColor = Color.Fuschia;
        dataGrid.GridStyle = new CustomDataGridStyle();
    }
}

 

SfDataGrid allows you to customize the rows for which the alternate row color needs to be updated based on the property SfDataGrid.AlternationCount.

dataGrid.AlternationCount = 3;

 

SfDataGrid also allows you to customize the AlternatingRowColor when changing the themes in runtime. This can be achieved by writing a custom style class for the SfDataGrid overriding from DataGridStyle class and returning the color need to be applied for alternate rows in the GetAlternatingRowBackgroundColor() override.

public class CustomDataGridStyle : DataGridStyle
{
    public CustomDataGridStyle()
    {
 
    }
 
    public override Color GetAlternatingRowBackgroundColor()
    {
        return Color.Yellow;
    }
}

 

Refer the below code example to know how to apply the custom style to SfDataGrid.

 
dataGrid.GridStyle = new CustomDataGridStyle ();

 

The following screenshot shows the final outcome upon execution of the above code

Calendar

The working sample for this KB is available in the following location

https://www.syncfusion.com/downloads/support/directtrac/general/ze/AlternateRowColor771799106

 

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