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

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

asp.net - gridview merge column headers

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

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

1 Answer

0 votes
by (71.8m points)

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);    
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...