Giter Site home page Giter Site logo

syncfusionexamples / how-to-create-wpf-datagrid-with-multiple-header-rows Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 6.0 672 KB

This example explains about how to create wpf datagrid with multiple header rows (stacked header rows).

C# 100.00%
wpf datagrid datagridview headers

how-to-create-wpf-datagrid-with-multiple-header-rows's Introduction

How to create wpf datagrid with multiple header rows (stacked header rows)?

About the sample

This example illustrates how to create SfDataGrid with multiple header rows using StackedHeaderRows.

WPF DataGrid (SfDataGrid) supports to add additional unbound header rows using StackedHeaderRows. This can be used to add multiple header rows for the SfDataGrid and to group one or more columns under each stacked header.

Each StackedHeaderRow contains StackedColumns where each StackedColumn contains a number of child columns. You can set the text displayed in the stacked column by using StackedColumn.HeaderText property.

<syncfusion:SfDataGrid AllowDraggingColumns="True" Margin="5"
                               ItemsSource="{Binding OrderList}">
    <syncfusion:SfDataGrid.StackedHeaderRows>
        <syncfusion:StackedHeaderRow>
            <syncfusion:StackedHeaderRow.StackedColumns>
                <syncfusion:StackedColumn ChildColumns="OrderID,OrderDate,RequiredDate,CustomerID,Customers.CompanyName,Customers.ContactName,Customers.Phone,ShippedDate,ShipCountry" HeaderText="Order Shipment Details" />
            </syncfusion:StackedHeaderRow.StackedColumns>
        </syncfusion:StackedHeaderRow>
        <syncfusion:StackedHeaderRow>
            <syncfusion:StackedHeaderRow.StackedColumns>
                <syncfusion:StackedColumn ChildColumns="OrderID,OrderDate,RequiredDate" HeaderText="Order Details" />
                <syncfusion:StackedColumn ChildColumns="CustomerID,Customers.CompanyName,Customers.ContactName,Customers.Phone" HeaderText="Customer Details" />
                <syncfusion:StackedColumn ChildColumns="ShippedDate,ShipCountry" HeaderText="Shipment Details " />
            </syncfusion:StackedHeaderRow.StackedColumns>
        </syncfusion:StackedHeaderRow>
    </syncfusion:SfDataGrid.StackedHeaderRows>

</syncfusion:SfDataGrid>

StackedHeaders

Stacked Headers using Data Annotation

It is possible to add the stacked headers using GroupName property of Data Annotations Display attributes.

[Display(GroupName = "Order Details", Name ="Order ID")]
public int OrderID
{
    get
    {
        return this._OrderID;
    }
    set
    {
        this._OrderID = value;
        this.OnPropertyChanged("OrderID");
    }
}

[Display(GroupName = "Order Details", Name ="Product Name")]
public string ProductName
{
    get
    {
        return this._product;
    }
    set
    {
        this._product = value;
        this.OnPropertyChanged("ProductName");
    }
}

[Display(GroupName = "Order Details")]
public int Quantity
{
    get
    {
        return this._Quantity;
    }
    set
    {
        _Quantity = value;
        OnPropertyChanged("Quantity");
    }
}

[Display(GroupName = "Order Details", Name ="Unit Price")]
[DataType(DataType.Currency)]
public double UnitPrice
{
    get
    {
        return _unitPrice;
    }
    set
    {
        _unitPrice = value;
        OnPropertyChanged("UnitPrice");
    }
}

[Display(GroupName = "Customer Details", Name ="Customer ID")]
public string CustomerID
{
    get
    {
        return this._CustomerID;
    }
    set
    {
        this._CustomerID = value;
        this.OnPropertyChanged("CustomerID");
    }
}

[Display(GroupName = "Customer Details", Name ="Contact Number")]
public int ContactNumber
{
    get
    {
        return this._contactNumber;
    }
    set
    {
        _contactNumber = value;
        OnPropertyChanged("ContactNumber");
    }
}

[Display(GroupName = "Customer Details")]
public string Country
{
    get
    {
        return this._shipaddress;
    }
    set
    {
        this._shipaddress = value;
        this.OnPropertyChanged("ShipCountry");

    }
}

StackedHeaders using DataAnnotation

Changing stacked header row height

You can change the height of stacked header rows by using VisualContainer.RowHeights property.

using Syncfusion.UI.Xaml.Grid.Helpers;

this.dataGrid.Loaded += DataGrid_Loaded;

private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
    var visualContainer = dataGrid.GetVisualContainer();
    int count = dataGrid.StackedHeaderRows.Count;

    for (int i = 0; i < count; i++)
    {
        visualContainer.RowHeights[i] = 50;
    }

    visualContainer.InvalidateMeasure();
}

You can also change the height of stacked header rows by using SfDataGrid.QueryRowHeight event.

using Syncfusion.UI.Xaml.Grid;

this.dataGrid.QueryRowHeight += DataGrid_QueryRowHeight;

private void DataGrid_QueryRowHeight(object sender, Syncfusion.UI.Xaml.Grid.QueryRowHeightEventArgs e)
{
    if (e.RowIndex < this.dataGrid.GetHeaderIndex())
    {
        e.Height = 50;
        e.Handled = true;
    }
}

Styling Stacked Headers

The appearance of stacked header can be customized by writing style with TargetType as GridStackedHeaderCellControl.

<Window.Resources>
     <Style TargetType="syncfusion:GridStackedHeaderCellControl">
         <Setter Property="FontWeight" Value="Bold"/>
         <Setter Property="FontSize" Value="14"/>
         <Setter Property="Foreground" Value="DarkBlue"/>
     </Style>
 </Window.Resources>

StackedHeaders Styling

KB article - How to create wpf datagrid with multiple header rows (stacked header rows)?

Requirements to run the demo

Visual Studio 2015 and above versions.

how-to-create-wpf-datagrid-with-multiple-header-rows's People

Contributors

9629976110 avatar backiaraj avatar farjanaparveen avatar mohanramanbukkarasu avatar sarubala20 avatar syncsiva avatar vinothkumar-ganesan avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.