gridview merge column headers

后端 未结 2 1861
無奈伤痛
無奈伤痛 2020-12-06 15:41

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 s

2条回答
  •  既然无缘
    2020-12-06 15:55

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

提交回复
热议问题