簡體   English   中英

將標題行添加到Gridview

[英]Adding header rows to Gridview

我的gridview與此類似:

 12- CategoryA - other columns 13- CategoryA - other columns 14- CategoryA - other columns 15- CategoryA - other columns 16- CategoryB - other columns 17- CategoryB - other columns 18- CategoryB - other columns 

我想要的是那樣的東西:

CategoryA (colspan 2) 
12 - other columns 
13 - other columns 
14 - other columns 
15 - other columns 
Category B (colspan 2) 
16 - other columns
17 - other columns 
18 - other columns

我可以通過修改我綁定為數據源的數據表來完成此操作嗎? 或者有更簡單的方法嗎?

我相信RowDataBound事件是你需要的。 您可以跟蹤顯示的先前類別以及將顯示的類別。

class MyClass
{
    private string CurrentCategory{ get; set; }

    // Load_Page with databinding the GridView{  }

    protected void mygridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow && 
          (e.Row.DataItem as DataRowView)["mycolumn"].ToString() != CurrentCategory))
        {
            GridViewRow tr = new GridViewRow(e.Row.RowIndex +1, e.Row.RowIndex + 1, 
                                   DataControlRowType.DataRow, e.Row.RowState);
            TableCell newTableCell = new TableCell();
            newTableCell.Text = (e.Row.DataItem as DataRowView)["mycolumn"].ToString();
            CurrentCategory = (e.Row.DataItem as DataRowView)["mycolumn"].ToString();
            tr.Cells.Add(newTableCell);

            ((Table)e.Row.Parent).Rows.Add(tr);
        }
    }
}

代碼按原樣提供,未經測試。

GridView控件不支持本機分組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM