在實際工作中,經常看到電子版或者紙質版的表格。這些表格,通常都不是一行表頭,需要2行或者3行,隻有這樣的表頭,才能明确表示表中資料的具體涵義。并且多行表頭中,将資料相同的單元格進行合并,包括橫向合并、縱向合并。
那麼,我們在使用Gridview時,如何才能實作多行表頭呢?
主要有三種方法,①是利用GridviwRow對象的header類型生成對象;②直接将表頭内容作為普通資料顯示,然後再通過樣式,将表頭的背景色與其它的行分開,模拟出表頭效果;③利用protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)事件,在生成用戶端的html時,生成需要的表格。
注意:這些操作,都是放在gridview綁定完資料這後執行,即放在Gridview1.DataSource=dt;Gridview1.Databind();之後執行。資料的列為自動綁定,沒有為每列指定要綁定的字段(AutoGenerateColumns屬性為true,預設)。
道理講完了,是不是想動手了,現在開始吧。
步1、将一個gridview控件拖放在頁面上
步2、實作多表頭
方法1:(适用于多行)
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLlxGdpR3LcljM3ATOwAjMvw1cldWYtlUeyRnbF9CX3YjbhlGdpF3LcRXZu9lbkN3Yfd2bsJ2Xw9CXzV2Zh1WavwFdl5mLuR2cj5yZvxmYtA3Lc9CX6MHc0RHaiojIsJye.jpg)
看完了效果,再來看看代碼吧
private void AddHeader()//為gridview添加表頭
{
if (GridView1.Rows.Count > 0)
{
GridView1.HeaderRow.Cells.Clear();
GridView1.HeaderRow.Visible = false;
}
Table tbl = (Table)GridView1.Controls[0];
#region 添加三行表頭
string ssql = "SELECT coltitle1,coltitle2,coltitle3 FROM Xt_Grid where (Grid_Code = 'CB_CostCollect')";
DataSet dgrid = DbHelperSQL.Query(ssql);
if (dgrid.Tables[0].Rows.Count > 0)
{
GridViewRow header0 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
GridViewRow header1 = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Normal);
GridViewRow header2 = new GridViewRow(2, 2, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < dgrid.Tables[0].Rows.Count; i++)
{
header0.Cells.Add(new TableCell());
header0.Cells[i].Wrap = false;
header0.Cells[i].Text = dgrid.Tables[0].Rows[i]["coltitle1"].ToString().Trim();
header1.Cells.Add(new TableCell());
header1.Cells[i].Wrap = false;
header1.Cells[i].Text = dgrid.Tables[0].Rows[i]["coltitle2"].ToString().Trim();
header2.Cells.Add(new TableCell());
header2.Cells[i].Wrap = false;
header2.Cells[i].Text = dgrid.Tables[0].Rows[i]["coltitle3"].ToString().Trim();
}
tbl.Controls.AddAt(0, header0);
tbl.Controls.AddAt(1, header1);
tbl.Controls.AddAt(2, header2);
}
#endregion
#region 合并表頭
for (int i = 0; i < dgrid.Tables[0].Rows.Count; i++)
{//合并行
TableCell oldtc = tbl.Rows[0].Cells[i];
for (int j = 1; j < 3; j++)
{
TableCell newtc = tbl.Rows[j].Cells[i];
if (newtc.Text == oldtc.Text)
{
newtc.Visible = false;
if (oldtc.RowSpan == 0)
{
oldtc.RowSpan = 2;
oldtc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldtc.RowSpan = oldtc.RowSpan + 1;
oldtc.VerticalAlign = VerticalAlign.Middle;
}
}
}
}
for (int i = 0; i < 2; i++)
{//合并列
TableCell oldtc = tbl.Rows[i].Cells[0];
for (int j = 1; j < dgrid.Tables[0].Rows.Count - 1; j++)
{
TableCell newtc = tbl.Rows[i].Cells[j];
if (newtc.Text == oldtc.Text)
{
newtc.Visible = false;
if (oldtc.ColumnSpan == 0)
{
oldtc.ColumnSpan = 2;
oldtc.HorizontalAlign = HorizontalAlign.Center;
}
else
{
oldtc.ColumnSpan++;
oldtc.HorizontalAlign = HorizontalAlign.Center;
}
}
else
{
oldtc = newtc;
}
}
}
#endregion
}
注意:表頭的名稱存放在了自建的一個表xt_grid裡面,對應的字段分别是:第一行表頭-ColTitle1,第二行表頭-ColTitle2,第三行表頭-ColTitle3。
說明:①清除、隐藏原來的表頭;②通過GridView1.Controls[0]擷取Table對象;③查找表頭資訊,此處為三行,循環生成GridViewRow對象,類型為頭(header),并添加到table中;④循環合并行,列。
方法二(為Gridview控件添加多行表頭(之二))
方法三(為Gridview控件添加多行表頭(之三))