Introducing Stacked Headers in Xamarin.Forms DataGrid | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Stacked Headers in Xamarin.Forms DataGrid

Introducing Stacked Headers in Xamarin.Forms DataGrid

To all the Syncfusion Xamarin.Forms DataGrid users and fans out there, we have some exciting news for you. The most awaited and requested feature has finally made its way in the 2019 Volume 2 release: stacked headers. For those of you who don’t know what stacked headers are, you may feel lost. To catch up, continue reading through this blog post, where I explain what stacked headers are, their capabilities, and why should you be excited about this feature’s arrival.

What are stacked headers?

Stacked headers are multiple-level column headers that span across multiple columns of the DataGrid. These unbound header rows take data visualization and data classification to the next level by allowing the columns in DataGrid to be grouped under common column headers. This enables you to present the data in a more pleasant, classified, and clear manner. The end users can then understand the data better, as the stacked headers clearly classify the data by grouping together the related columns. Thus, you can easily convey to the end user what type of information these columns are going to hold and display. Now let’s add some stacked headers and begin simplifying data for the end user.

<sfgrid:SfDataGrid.StackedHeaderRows>
    <sfgrid:StackedHeaderRow>
        <sfgrid:StackedHeaderRow.StackedColumns>
            <sfgrid:StackedColumn
                ChildColumns="OrderID,OrderDate,CustomerID,ContactName"
                Text="Order Shipment Details"
                TextSize="{StaticResource textSize}"
                MappingName="SalesDetails"
                FontAttribute="Bold"
                TextAlignment="Center"
            />
        </sfgrid:StackedHeaderRow.StackedColumns>
    </sfgrid:StackedHeaderRow>
 
    <sfgrid:StackedHeaderRow>
        <sfgrid:StackedHeaderRow.StackedColumns>
            <sfgrid:StackedColumn
                ChildColumns="OrderID,OrderDate"
                Text="Order Details"
                TextSize="{StaticResource textSize}"
                MappingName="OrderDetails"	
                FontAttribute="Bold"
                TextAlignment="Center"
            />
            <sfgrid:StackedColumn
                ChildColumns="CustomerID,ContactName"
                Text="Customer Details"
                TextSize="{StaticResource textSize}"
                MappingName="CustomerDetails"
                FontAttribute="Bold"
                TextAlignment="Center"
            />
        </sfgrid:StackedHeaderRow.StackedColumns>
    </sfgrid:StackedHeaderRow>
</sfgrid:SfDataGrid.StackedHeaderRows>
var stackedHeaderRow = new StackedHeaderRow();
stackedHeaderRow.StackedColumns.Add(new StackedColumn()
{
	ChildColumns = "OrderID" + "," + "OrderDate" + "," + "CustomerID" + "," + "ContactName",
	Text = "Order Shipment Details",
	MappingName = "SalesDetails",
	FontAttribute = FontAttributes.Bold,
	TextAlignment = TextAlignment.Center,
});
dataGrid.StackedHeaderRows.Add(stackedHeaderRow);

var stackedHeaderRow1 = new StackedHeaderRow();
stackedHeaderRow1.StackedColumns.Add(new StackedColumn()
{
	ChildColumns = "OrderID" + "," + "OrderDate",
	Text = "Order Details",
	MappingName = "OrderDetails",
	FontAttribute = FontAttributes.Bold,
	TextAlignment = TextAlignment.Center
});
stackedHeaderRow1.StackedColumns.Add(new StackedColumn()
{
	ChildColumns = "CustomerID" + "," + "ContactName",
	Text = "Customer Details",
	MappingName = "CustomerDetails",
	FontAttribute = FontAttributes.Bold, 
	TextAlignment = TextAlignment.Center
});
this.dataGrid.StackedHeaderRows.Add(stackedHeaderRow1);

Ta-da! Stacked headers in an Android device

Ta-da! Stacked headers in an Android device

Why should you be excited?

Appearance customizations (foreground and background)

Apart from the awesomeness of simplifying data, this feature comes with loads of preloaded customization options to match the dashing, attractive UI of your application. You get to play with the font face, font color, font size, and background color of the rows; the border color of the cells; etc.

////---------------------StackedHeaderRowOne-----------------------------////
StackedHeaderRow _stackedHeaderRowOne = new StackedHeaderRow();

StackedColumn _stackedColumn = new StackedColumn();
_stackedColumn.ChildColumns = "OrderID" + "," + "ShippingDate" + "," + "EmployeeID" + "," + "FirstName" + "," + "ShipCity" + "," + "ShipCountry";
_stackedColumn.Text = "Order Shipment Details";
_stackedColumn.TextSize = 40;
_stackedColumn.MappingName = "SalesDetails";
_stackedColumn.Font = "Amontillados.ttf#Amontillados";
_stackedHeaderRowOne.StackedColumns.Add(_stackedColumn);

////---------------------StackedHeaderRowTwo-----------------------------////
StackedHeaderRow _stackedHeaderRowTwo = new StackedHeaderRow();

////----------------StackedColumnOne-------------------////
StackedColumn stackedColumnOne = new StackedColumn();
stackedColumnOne.ChildColumns = "OrderID" + "," + "ShippingDate";
stackedColumnOne.Text = "Order Details";
stackedColumnOne.TextSize = 12;
stackedColumnOne.MappingName = "OrderDetails";
stackedColumnOne.FontAttribute = FontAttributes.Bold;
stackedColumnOne.Font = "Arial";

////----------------StackedColumnTwo-------------------////
StackedColumn stackedColumnTwo = new StackedColumn();
stackedColumnTwo.ChildColumns = "EmployeeID" + "," + "FirstName";
stackedColumnTwo.Text = "Customer Detail";
stackedColumnTwo.TextSize = 12;
stackedColumnTwo.MappingName = "CustomerDetails";
stackedColumnTwo.FontAttribute = FontAttributes.Bold;
stackedColumnTwo.Font = "Italic";

_stackedHeaderRowTwo.StackedColumns.Add(stackedColumnOne);
_stackedHeaderRowTwo.StackedColumns.Add(stackedColumnTwo);

StackedHeaderRowCollection _stackedHeaderRowCollection = new StackedHeaderRowCollection();
_stackedHeaderRowCollection.Add(_stackedHeaderRowOne);
_stackedHeaderRowCollection.Add(_stackedHeaderRowTwo);

dataGrid.StackedHeaderRows = _stackedHeaderRowCollection;

dataGrid.GridStyle = new MyStyle();


public class MyStyle : DataGridStyle
{
    public MyStyle()
    {
    }

    public override Color GetStackedHeaderBackgroundColor(int rowIndex)
    {
            return Color. OrangeRed;
    }

    public override Color GetStackedHeaderForegroundColor(int rowIndex)
    {
            return Color.FromRgb(255, 255, 255);
    } 

   public override Color GetHeaderBorderColor()
   {
         return Color.Black;
    }
}

Customized stacked headers

Customized stacked headers

As a step further, you can also apply a different background color to each row of the stacked headers conditionally, based on its row index.

dataGrid.GridStyle = new MyStyle();

public class MyStyle : DataGridStyle
{
    public MyStyle()
    {
    }

    public override Color GetStackedHeaderBackgroundColor(int rowIndex)
    {
        // Applying background color based on row index.
        if (rowIndex == 0)
        {
            return Color.Black;
        }
        else if (rowIndex == 1)
        {
            return Color.DarkOliveGreen;
        }
    }

    public override Color GetStackedHeaderForegroundColor(int rowIndex)
    {
        // Applying foreground color based on row index.
        if (rowIndex == 0 || rowIndex == 1)
        {
            return Color.FromRgb(255, 255, 255);
        }
        else
        {
            return Color.FromRgb(43, 43, 43);
        }
    } 
}

Conditional styling in stacked headers

Conditional styling in stacked headers

View customizations

Even more exciting, the customization options are left entirely to you. It’s time to awaken the UI developer in you and get creative, because with built-in templates and template selector support for the stacked header rows, the customizations are completely within your control.

<syncfusion:SfDataGrid.StackedHeaderRows>
            <syncfusion:StackedHeaderRow>
                <syncfusion:StackedHeaderRow.StackedColumns>
                    <syncfusion:StackedColumn
                            ChildColumns="OrderID,OrderDate"
                            Text="Order Details"
                            MappingName="OrderDetails"
                            FontAttribute="Bold"
                            TextAlignment="Center"
                            />
                    <syncfusion:StackedColumn
                            ChildColumns="CustomerID,ContactName"
                            MappingName="CustomerDetails"
                            >
                            <syncfusion:StackedColumn.Template>
                            <DataTemplate>
                                <Grid BackgroundColor="MediumPurple">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="50"/>
                                    </Grid.ColumnDefinitions>
                                    <Label Text="Customer Details" TextColor="White" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Column="0"/>
                                    <Image Source="customer_details.png"  VerticalOptions="Center" HorizontalOptions="Start" Aspect="AspectFit" Grid.Column="1"/>
                                </Grid>
                            </DataTemplate>
                        </syncfusion:StackedColumn.Template>
                     </syncfusion:StackedColumn>
                </syncfusion:StackedHeaderRow.StackedColumns>
            </syncfusion:StackedHeaderRow>
      </syncfusion:SfDataGrid.StackedHeaderRows>

Template content in stacked headers

Template content in stacked headers

Height customizations

Let’s move on to the available height customization options for the stacked headers. You can choose to have the same height for all the header rows, have different heights for each header row, or even auto-size the header row height based on its content.

Zero limitations

Are you developing an application to display the prolific results of a complex scientific experiment? Stacked headers has you covered! You can add any number of rows and columns in any combination without any limitations or restrictions to display data in any way you wish.

// Use below code to apply same height to all header rows.
dataGrid.HeaderRowHeight = 50;
dataGrid.QueryRowHeight += dataGrid_QueryRowHeight;
void dataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs  e)
{
    // Use the below code to set a desired height based on the row index. 
    if(e.RowIndex == 0)
    {
        e.Height = 50;
    }
    else if(e.RowIndex == 1)
    {
        // Use the below code to apply auto height based on the contents of the stacked header row.
        e.Height = dataGrid.GetRowHeight(e.RowIndex);
    }
    else
    {
        e.Height = 70;
    }
    e.Handled = true;
}

Large Grid

Conclusion

In addition to this, stacked headers support interactive column-width resizing and CRUD operations. Therefore, runtime changes to the UI are a piece of cake with no additional overhead required. Thus summing up why stacked headers was the most requested feature in DataGrid. We hope you play around with this feature yourself, available from this 2019 Volume 2 release. This documentation link might come in handy, just in case. If you want more info on the DataGrid, look no further.

I hope you found this blog interesting. If you still aren’t excited, I am sure you will be when you check out the awesome controls in our Xamarin suite. Do let me know your thoughts in the comments section below.

As always, you can also contact us through our support forumDirect-Trac, or feedback portal and we will be happy to assist you.

Tags:

Share this post:

Comments (4)

Looks great, but some samples using MVVM would be a great addition to this post.
Not just what the markup for the StackedHeaderRow but markup and data binding samples for a full grid.

Hi @Gary,

Thanks for your valuable feedback. As per your suggestion, posting the link of the sample below.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfGridSample-2094173312

What about single fixed header row?
I can’t show them like a simple grid with fixed header.

Hi @Christian,

We noticed that you have created a support ticket for the same via this forum (https://www.syncfusion.com/forums/148554/fixed-header-row). Our support team will follow up with it and provide solutions in that thread. Thank you !

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed