Header Spanning 2 Columns GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yoda
    Contributor
    • Dec 2006
    • 291

    Header Spanning 2 Columns GridView

    Hello Everyone,

    I've scoured internet looking for a way to span a heading over over 2 columns and to no avail. There are many solutions but none are what I'm looking for.

    I've attached a excel file with what the gridview should look like.

    If you look there are headers that span 2 columns. The columns with Count and Balance should be under these headers.

    All the columns in the table are dynamically bound during run time, meaning I create them in code. So with that in mind.

    Is there a simple way of doing this?

    I've tried using a Gridview row and then adding tablecells and spanning the cells 2 columns but when you export the file it looks like garbage.

    Any help would be appreciated!

    Thanks,
    Yoda.
    Attached Files
    Last edited by yoda; Feb 24 '12, 11:57 PM. Reason: More explanation. Took away my code for exporting a gridview to excel.
  • yoda
    Contributor
    • Dec 2006
    • 291

    #2
    Figured it out. Ended up just adding another header row to the to the GridView and adding TableCells to it and calling the TableCell.Colum nSpan property and setting it to 2.

    Example:

    Code:
    //Create a new Header Row
    GridViewRow HeaderGridRow =
                new GridViewRow(0, 0, DataControlRowType.Header,DataControlRowState.Normal);
    
    //Create TableCells
    TableCell HeaderCell = new TableCell();
    HeaderCell.Text = text;
    HeaderCell.ColumnSpan = colspan;
    HeaderGridRow.Cells.Add(HeaderCell);
    
    //Add new Header Row to Gridview
    GridView1.Controls[0].Controls.AddAt
                (0, HeaderGridRow);

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I would have approached this a lot differently than you have.

      First of all, I would never use Response.Write in my C# or VB.NET code behind because the location of where the writes take place will likely be out side of the HTML <body> tag. This will make your page's HTML invalid.

      Instead I would use ASP.NET controls that I could access in my code behind to set the styles for the controls.

      That's kind of besides the point.

      The other thing that I would do a lot differently is create a TemplateField for my columns within the GridView. If you need to have dynamic columns (ie: the number of columns in your GridView is going to change) then I would create a TemplateField class to use in my dynamic code (this is pretty advanced though).

      So, to keep things simple, in the ASP mark up (not the C# or VB.NET code behind) I would define template fields for my columns and bind the controls within these templates to the data that I am expecting to be binding to the GridView.

      This way I could display 2 pieces of bound information in one column.

      -Frinny

      Comment

      • yoda
        Contributor
        • Dec 2006
        • 291

        #4
        Unfortunately this program hasn't been developed by me, and hasn't been built from the ground up. So I can't really say how valid or invalid the HTML is and I'm going of off what was already there and google. I've never programmed in ASP.NET or C#(Though it's very similar to Java) so this is all new to me, and haven't figured out the limitations of ASP since I've rarely programmed for the web.

        But thank you for the insight on how you would do it. I'm only been programming for the last 2 years or so as a Computer Programmer Analyst student. So I have quite a bit to learn yet and haven't really narrowed myself down to specific field of study.

        But thank you again. Also if you don't mind me asking how would you of done exporting of a GridView then in ASP.NET 2.0 with all the colours and such stylings?

        Thanks,
        Yoda.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Ok, I think I'm confused about what your problem is.

          Are you having a problem exporting the data in a particular format?

          Or are you having a problem displaying the data in web browser within the GridView?

          I haven't had the pleasure of exporting to excel but I have exported data to a .cvs format. I took the datasource for the GridView and parsed it into Comma-Separated-Values (following the rules on what the format should be to create a valid csv file).

          Excel would probably require the same thing but I would be creating a Microsoft Excel file object and putting the data from the DataSource into that instead.

          -Frinny

          PS I cannot open your excel file with the netwrok settings that are imposed on me.

          Also, if you are exporting, it's perfectly valid to use the response.write since you aren't sending HTML back to the browser.
          Last edited by Frinavale; Feb 24 '12, 08:14 PM.

          Comment

          • yoda
            Contributor
            • Dec 2006
            • 291

            #6
            Yea sorry I add how I export my GridView to an excel for some reason, but non in particular. I should probably edit that post to take that out.

            Yea I originally I had trouble with my GridView viewing the columns properly here's the format with out the excel:

            Col 1 ---- Col 2 ----- Col 3 -------------A---------------B--------------C--------------D------
            |--Col1--||--Col 2--||--Col 3--|----|| Cnt | Bal ||--|| Cnt | Bal ||--|| Cnt | Bal ||--|| Cnt | Bal ||


            The first three header columns only have one bound column, where A to D all have 2 sub columns that are data bound Count and Balance. I had trouble originally getting the header columns to span over 2 columns, so my solution was to make the count and balance header columns, then add another header row on top of the gridview and then make table cells and add them to the new header row and make that cell span 2 columns using the TableCell.Colum nSpan property.


            Yea I've created a csv before and even had the challenge of zipping a csv while it's still on the server before it hits the client side for download, that was interesting.

            Thanks for the info!
            Yoda.

            Comment

            Working...