Click here to Skip to main content
6,295,667 members and growing! (12,813 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Data     Intermediate License: The Code Project Open License (CPOL)

Dynamic Multiple Row Column Grid Header

By Somnath Pal

Creating dynamic header for DataGrid or GridView with multiple row/column in ASP.NET
C# (C# 3.0).NET 3.5, ASP.NET, Dev
Posted:15 Jul 2008
Views:15,821
Bookmarked:24 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 2.96 Rating: 3.50 out of 5
1 vote, 14.3%
1

2
2 votes, 28.6%
3
1 vote, 14.3%
4
3 votes, 42.9%
5

Introduction

Let’s assume we have a product table as shown below.

ProductIdProductNameParentProductId
1SOAPNULL
2 SHAMPOONULL
3Bath Soap1
4Pink Soap3
5White Soap3
6Toilet Soap1

As it can be seen, the depth of header is 3. This depth can be any number i.e. there is no limit of hierarchy depth. Our objective is to add a header at the top of a GridView or DataGrid as shown below

SOAPSHAMPOO
Bath SoapToilet Soap
White SoapPink Soap

Background

The approach I have taken is to draw table cells with appropriate row and column span in the header of DataGrid/GridView. So the problem can be minimized as how to create a collection of table cells with appropriate row and column span to get the desire effect.

1st Step: We need to create a header string as SOAP|Bath Soap|Pink Soap,SOAP|Bath Soap|White Soap,SOAP|Toilet Soap,SHAMPOO If we take a close look at the string we will find that each product has been formed as a ‘|’ (pipe) separated string with all its parents. Then all these product strings are concatenated as a header string using ‘,’ as separator. In this article the string was hard coded. But in actual development, the string can be formed from the database using a Stored Procedure.

2nd Step: We will examine each product string and find out the maximum occurrences of ‘|’. This number plus 1 (say M) will be the number of rows of the header. In this example M=3. Finding out number of columns (say N) is simple. It will be the number of product strings and in this example N=4.

3rd Step: We will create a M by N matrix and put the header strings as below.

SOAPSOAPSOAPSHAMPOO
Bath SoapBath SoapToilet Soap
White SoapPink Soap

4th Step: We will compare the contents of two adjacent cells for each row. It the contents are same, we will increase the column span of 1st cell by 1 and delete the content of 2nd cell. Then we will compare the contents of two adjacent cells for each column. If cell1 has value and cell2 doesn’t have any value, we will increase the row span of 1st cell by 1. For each row we will create a collection of headers with calculated column and row span. In this example there will be such 3 collections. Then we will put these collections into a container collection for rendering.

5th and final step: We will render the table cells with row and column span to achieve our goal. While rendering, we can control table cell properties for a better look & fill. In this example, I have used different background color and font size for different level of header. I have also added an xml file with some data (say sales data of the products) and the final outcome is

SOAPSHAMPOO
Bath SoapToilet Soap
Pink SoapWhite Soap
100200300400
10203040
1234
100200300400

Using the code

I have created 3 classes to parse the header string and create the collection to render.

  • DynamicHeaderCell
  • DynamicHeader
  • DynamicHeaders

DynamicHeaderCell


public class DynamicHeaderCell
{
    public String Header { get; set; }
    public int RowSpan { get; set; }
    public int ColSpan { get; set; }
    
    public DynamicHeaderCell(String header)
    {
        RowSpan = 1;
        ColSpan = 1;
        Header = header;
    }
}

Objects of this class are used to generate the collection. Header string will be content of TableCell and RowSpan and ColSpan will be used to set the RowSpan and ColumnSpan of the table cell.

DynamicHeader


public class DynamicHeader
{
    public int HeaderDepth { get; set; }
    public String[] Headers { get; set; }
    public DynamicHeader(String header)
    {
        Headers = header.Split('|');
        HeaderDepth = Headers.Length;
    }
}

Each individual product string is parsed in this class. The depth is stored in the HeaderDepth.

DynamicHeaders


public class DynamicHeaders
{
    List Headers;
    int HeaderRows;
    int HeaderCols;

    public DynamicHeaders(String Header)
    {
        Headers = new List();
        String[] HeaderParts = Header.Split(',');
        foreach (String tmpHeaderPart in HeaderParts)
            Headers.Add(new DynamicHeader(tmpHeaderPart));

        HeaderCols = Headers.Count;
        HeaderRows = Headers.Max(H => H.HeaderDepth);
        ParseHeader();
    }

    public ArrayList ParseHeader()
 }

Header string is parsed in this class. ParseHeader() method implements the logic discussed above and returns the collection for rendering.

Rendering


protected void GridViewWithDynamicHeader_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.Header)
            //Render the header
            e.Row.SetRenderMethodDelegate(new RenderMethod(RenderHeader));
    }

In the GridView Row created method, header rendering has been delegated to the RenderHeader method. In the RenderHeader method, ParseHeader method of DynamicHeaders class is called and TableCells are rendered from the collection recieved.

History

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 ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
QuestionASP.net 1.1 Pinmemberanjanrajkumar22:00 28 May '09  
AnswerRe: ASP.net 1.1 [modified] PinmemberSomnath Pal0:05 29 May '09  
GeneralRe: ASP.net 1.1 Pinmemberanjanrajkumar16:07 31 May '09  
GeneralRe: ASP.net 1.1 PinmemberSomnath Pal21:56 31 May '09  
GeneralRe: ASP.net 1.1 Pinmemberanjanrajkumar17:05 31 May '09  
GeneralCompilation Error PinmemberXixao10:50 31 Mar '09  
GeneralRe: Compilation Error PinmemberSomnath Pal0:19 29 May '09  
GeneralDynamic Multiple Row Column Grid Header Pinmemberqpqpqp12:25 3 Sep '08  
AnswerRe: Dynamic Multiple Row Column Grid Header PinmemberSomnath Pal23:51 3 Sep '08  
GeneralNice PinmemberZeroDev0:34 22 Jul '08  
GeneralSorting... PinmemberBasel Nimer6:21 16 Jul '08  
GeneralGreat idea PinmemberFakher Halim5:11 16 Jul '08  

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

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