Articles in this section
Category / Section

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

1 min read

You can set the different background color for each column header in Xamarin DataGrid by adding the TextView to the column header using GridColumn.HeaderTemplate property.

The below code illustrates how to set the different background color for each columns header in SfDataGrid.

 
private SfDataGrid datagrid;
private ViewModel viewModel;
private TextView textView;
private TextView textView1;
private TextView textView2;
private TextView textView3;
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    datagrid = new SfDataGrid(BaseContext);
    viewModel = new ViewModel();
    textView = new TextView(this);
    textView1 = new TextView(this);
    textView2 = new TextView(this);
    textView3 = new TextView(this);
    datagrid.ItemsSource = viewModel.OrdersInfo;
    datagrid.ColumnSizer = ColumnSizer.Star;
    datagrid.AutoGenerateColumns = false;
            
    textView.Text = "OrderID";
    textView.Gravity = Android.Views.GravityFlags.Center;
    textView.SetBackgroundColor(Color.SandyBrown);
    textView1.Text = "CustomerID";
    textView1.Gravity = Android.Views.GravityFlags.Center;
    textView1.SetBackgroundColor(Color.Crimson);
    textView2.Text = "Freight";
    textView2.Gravity = Android.Views.GravityFlags.Center;
    textView2.SetBackgroundColor(Color.Gray);
    textView3.Text = "Country";
    textView3.Gravity = Android.Views.GravityFlags.Center;
    textView3.SetBackgroundColor(Color.Blue);
    GridTextColumn column = new GridTextColumn()
        {
            MappingName = "OrderID",
            HeaderTemplate = textView 
        };
    GridTextColumn column1 = new GridTextColumn()
    {
        MappingName = "CustomerID",
        HeaderTemplate = textView1
    };
    GridTextColumn column2 = new GridTextColumn()
    {
        MappingName = "Freight",
        HeaderTemplate = textView2
    };
    GridTextColumn column3 = new GridTextColumn()
    {
        MappingName = "Country",
        HeaderTemplate = textView3
    };
    datagrid.Columns.Add(column);
    datagrid.Columns.Add(column1);
    datagrid.Columns.Add(column2);
    datagrid.Columns.Add(column3);
    SetContentView(datagrid);

  

Screenshot:

Styles in Xamarin.Android Grid

Sample Link:

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


Conclusion

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

You can refer to our Xamarin.Android DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Android 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