Articles in this section
Category / Section

How to set different background color for each column header in Xamarin.iOS DataGrid?

3 mins read

You can set different background color for each column header by customizing the GridColumn.HeaderTemplate in SfDataGrid.

Refer the below code in which an UILabel is loaded inside the header template of the columns and its background color is updated based on requirement.

 

For Auto generated columns:

public MyViewController()
{
       dataGrid = new SfDataGrid();
       sviewModel = new ViewModel();
 
       dataGrid.ItemsSource = viewModel.OrdersInfo;
       dataGrid.ColumnSizer = ColumnSizer.Star;
       dataGrid.AutoGenerateColumns = true;
       dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;
 
       this.View.AddSubview(dataGrid);
}
 
private void DataGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
     if (e.Column.MappingName == "OrderID")
     {
         e.Column.HeaderTemplate = new UILabel()
         {
              Text = "OrderID",
              BackgroundColor = UIColor.Gray,
              TextAlignment=UITextAlignment.Center
         };
      }
      else if (e.Column.MappingName == "CustomerID")
      {
          e.Column.HeaderTemplate = new UILabel()
          {
               Text = "CustomerID",
               BackgroundColor = UIColor.Brown
               TextAlignment=UITextAlignment.Center
           };
      }
      else if (e.Column.MappingName == "Freight")
      {
           e.Column.HeaderTemplate = new UILabel()
           {
                Text = "Freight",
                BackgroundColor = UIColor.Green
                TextAlignment=UITextAlignment.Center
           };
      }
      else if (e.Column.MappingName == "Country")
      {
           e.Column.HeaderTemplate = new UILabel()
           {
                Text = "Country",
                BackgroundColor = UIColor.Red
                TextAlignment=UITextAlignment.Center
            };
      }
}
 
public override void ViewDidLayoutSubviews()
{
      base.ViewDidLayoutSubviews();
      dataGrid.Frame = new CoreGraphics.CGRect(0, 20, this.View.Frame.Width, this.View.Frame.Height);
}

 

For Manually generated columns:

public MyViewController()
{
       dataGrid = new SfDataGrid();
       sviewModel = new ViewModel();
 
       dataGrid.ItemsSource = viewModel.OrdersInfo;
       dataGrid.ColumnSizer = ColumnSizer.Star;
       dataGrid.AutoGenerateColumns = false;
 
       dataGrid.Columns.Add(new GridTextColumn()
       {
            MappingName = "OrderID",
            HeaderTemplate=new UILabel()
            {
                       Text="OrderId",
                        BackgroundColor=UIColor.Gray,
                        TextAlignment=UITextAlignment.Center
             } 
       };
                                  
       dataGrid.Columns.Add(new GridTextColumn()
       {
               MappingName = " CustomerID ",
               HeaderTemplate=new UILabel()
               {
                          Text=" CustomerID ",
                          BackgroundColor=UIColor. Brown,
                          TextAlignment=UITextAlignment.Center
                }
        }; 
                                  
        dataGrid.Columns.Add(new GridTextColumn()
        { 
               MappingName = " Freight ",
               HeaderTemplate=new UILabel()
               {
                          Text=" Freight ",
                           BackgroundColor=UIColor. Green,
                           TextAlignment=UITextAlignment.Center
                }
         }; 
                                 
       dataGrid.Columns.Add(new GridTextColumn()
       {  
               MappingName = " Country ",
               HeaderTemplate=new UILabel()
               {
                          Text=" Country ",
                          BackgroundColor=UIColor. Red,
                          TextAlignment=UITextAlignment.Center
                } 
        };
                                 
        this.View.AddSubview(dataGrid);
}
 
public override void ViewDidLayoutSubviews()
{
      base.ViewDidLayoutSubviews();
      dataGrid.Frame = new CoreGraphics.CGRect(0, 20, this.View.Frame.Width, this.View.Frame.Height);
}

 

 

 

On executing the above code the below output will be obtained.

C:\Users\pavithra.sivakumar\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Simulator Screen Shot 08-Feb-2017, 12.14.06 PM.PNG

Sample Link

How to set different background color for each column header in SfDataGrid?

 

Conclusion

I hope you enjoyed learning about how to set different background color for each column header in Xamarin.iOS DataGrid.

You can refer to our Xamarin.iOS DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.iOS DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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