直接在建立資料列時就做表頭的合併動作

 

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.Header)
  {
    // 建立自訂的標題
    GridView gv = (GridView)sender;

    GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
    GridViewRow gvRow1 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
    
    // 增加欄位
    TableCell tc1 = new TableCell();
    tc1.Text = "xxxx";
    gvRow.Cells.Add(tc1);

    TableCell tc2 = new TableCell();
    tc2.Text = "xxxx";
    gvRow.Cells.Add(tc2);

    TableCell tc3 = new TableCell();
    tc3.Text = "xxxx";
    gvRow.Cells.Add(tc3);

    
    TableCell tc4 = new TableCell();
    tc4.Text = "xxxx";
    tc4.ColumnSpan = 2;   // 跨二欄
    gvRow1.Cells.Add(tc4);

    
    TableCell tc5 = new TableCell();
    tc5.Text = " ";
    gvRow1.Cells.Add(tc5);
                
    gvRow1.BackColor = System.Drawing.Color.White;
    gvRow1.ForeColor = System.Drawing.Color.Black;

    // 先清除原標題所有內容
    e.Row.Cells.Clear();

    // 加入自訂標題
    gv.Controls[0].Controls.AddAt(0, gvRow);
    gv.Controls[0].Controls.AddAt(1, gvRow1);
  }
}

結果:

20110322001.jpg

參考:

http://teacher.syset.com/viewtopic.mspx?t=3085

arrow
arrow
    全站熱搜

    鴨爸 發表在 痞客邦 留言(0) 人氣()