Click here to Skip to main content
6,305,776 members and growing! (14,928 online)
Email Password   helpLost your password?
Web Development » Silverlight » Controls     Intermediate License: The Code Project Open License (CPOL)

Silverlight Data Grid with Dynamic Multiple Row Column in Header

By Somnath Pal

Creating dynamic header for Silverlight DataGrid with multiple row/column
C# (C# 3.0), .NET (.NET 3.0), Silverlight, Dev
Posted:15 Dec 2008
Views:11,072
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 1.10 Rating: 3.67 out of 5

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5

Introduction

I was developing a product in 2006 and one of the requirement was to create a Data Grid with mutiple columns and rows in the header. In my previous article Dynamic Multiple Row Column Grid Header I have presented the idea I have used. Last few months I was exploring Silverlight and I was wondering how the same can be achieved using Silverlight Data Grid. I searched internet looking for a solusion but couldn't find any. So I came up with a very crude way to do this. I am looking for any better solution. Also another feature I wanted is to export the same in Excel. At this point I don't know how to do that.

Background

So the goal of this experiment is to achieve the following output using Silverlight Data Grid.

DynamicMutipleRowHeaderSilverlighGrid

I have used the similar technique as decribed in my previous article to calculate the row and column span of the headers. I am skipping the logic here. This time I also need a footer. So the approach I have taken is that I will create a Grid of MxN size dynamically. M will be no of header rows plus 1 for the data grid plus 1 for the footer i.e. M = No of header rows + 2. N will be equal to number of data grid columns. I will put the footer details in last row of the grid, Data Grid in the last but 1 row of the grid and all the header items in the remaining rows with approprite row and col span.

Using the code

I have used the same 3 classes (described in the previous article) to parse the header string and create the collection to render.

  • DynamicHeaderCell
  • DynamicHeader
  • DynamicHeaders
I have added another class RowHeader and instead of a ArrayList I have used List of RowHeader to get the collection of header cells after parse. I have added another class SalseData to get the necessary data. This class has 4 static methods
  • GetSalseData - Data to display in data grid
  • GetSalseDataHeader - Header string
  • GetSalseDataColumnHeader - To create data grid columns
  • GetSalseDataFooter - Data to display in footer

Hard coded strings are used in the above functions. In real appliction the methods can get the data from database or any other source.

The xaml code is simple as generated by Visual Studio. All the work is done dynamically in the code behind.

Following Member variables are defined in the code behind

        DataGrid grdData = new DataGrid() { AutoGenerateColumns = false };
        bool bGridInitialized = false;
        List Headers = null;

Three methods are defined in the code behind

  • InitUI
  • grdData_LayoutUpdated
  • AddHeaderTextBlock

In the method InitUI()

A) Header is parsed and stored in Headers collection

            String[] ColHeaders = SalseData.GetSalseDataColumnHeader().Split(',');
            Headers = dynHead.ParseHeader();
 

B) Data Grid columns are created and data source added

            foreach (String col in ColHeaders)
            {
                String[] colHeader = col.Split('|');
                grdData.Columns.Add(new DataGridTextColumn { Header = colHeader[0], 
                Binding = new System.Windows.Data.Binding(colHeader[1]), 
                IsReadOnly = true });
            }
            grdData.ItemsSource = SalseData.GetSalseData();

C) Grid rows are created

In the method grdData_LayoutUpdated()

A) Grid Columns are created

            for (int i = 0; i < grdData.Columns.Count; i++)
            {
                DataGridColumn dgc = grdData.Columns[i];
                LayoutRoot.ColumnDefinitions.Add(...);
            }

B) AddHeaderTextBlock method is called to add the header and footer text

C) Data grid is positioned

In the method AddHeaderTextBlock()

Border with proper border thickness and text block with header text are added to the appropriate cell of the grid

History

Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Somnath Pal


Member

Occupation: Software Developer (Senior)
Company: TCG Software Services Pvt. Ltd.
Location: India India

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralNow if you could only... PinmemberDewey21:25 16 Dec '08  
GeneralRe: Now if you could only... PinmemberSomnath Pal23:34 16 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Dec 2008
Editor:
Copyright 2008 by Somnath Pal
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project