Articles in this section
Category / Section

How to apply alternate row colors in Xamarin.iOS DataGrid?

2 mins 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 by default. On specifying a color value for this property the SfDataGrid appears with alternate row colors.

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

public partial class ViewController : UIViewController
{
   SfDataGrid grid=new SfDataGrid);
   ViewModel viewmodel = new ViewModel ();
   public override void ViewDidLoad ()
   {
      base.ViewDidLoad ();
      grid.ItemsSource=viewmodel.Info;
      grid.BackgroundColor = UIColor.White;
      grid.AlternationCount = 2;
      grid.GridStyle.AlternatingRowColor = UIColor.Red;
      View.AddSubview (grid);
   }
}

 

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

grid.AlternationCount = 3;

 

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

public class CustomGridStyle : DataGridStyle
{
   public override Color GetAlternatingRowBackgroundColor()
   {
      return UIColor.Cyan;
   }
}

 

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

grid.GridStyle = new CustomGridStyle ();

 

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

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