Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a gridview and I want to merge the headers of column (n) and (n+1) into one so that the output will use colspan to show a single header for columns n and n+1. I've seen several ways to do it by googling for this technique but all seem to be rather cumbersome and verbose.

I was wondering if someone had come across an elegant a short solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
407 views
Welcome To Ask or Share your Answers For Others

1 Answer

Have you seen this article "Merge Two GridView Header columns in ASP.NET"? It builds the header row as you see fit. When you do this, you'll have to explicitly build each header (i.e. if you're merging Customer First Name and Customer Last Name, you'll have to ensure you explicitly include Customer ID and Customer Address, etc. as required.)

protected void myGridView_ItemCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {   
         //custom header.
         GridView gvHeader = (GridView)sender;
         GridViewRow gvHeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
         TableCell tc1 = new TableCell();

         // First column
         tc1.Text = "Merged Column1";
         tc1.ColumnSpan = 2;
         tc1.BackColor = System.Drawing.Color.Brown;               
         gvHeaderRow.Cells.Add(tc1);

         // Second  column    
         tc1 = new TableCell();
         tc1.Text = "Merged Column2";
         tc1.ColumnSpan = 2;
         gvHeaderRow.Cells.Add(tc1);
         //keep building as needed.
         gvHeader.Controls[0].Controls.AddAt(0, gvHeaderRow);    
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...