2013-04-02 76 views
0

我的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 

我要的是smthing這樣的:

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 

我能做到這一點通過對矯正的數據表,其我綁定爲數據源?還是有更簡單的方法?

+0

'ListView'中的'gridview'? –

+0

@HseseinNarimaniRad對不起,我不明白? – HOY

+0

你可以發佈你的XAML的一些部分? –

回答

2

我相信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); 
     } 
    } 
} 

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

+0

這似乎與一些修改工作,它添加標題,但在行下面。你能修改它嗎?我不知道gridviewrow參數的工作原理 – HOY

+0

互聯網的奇蹟:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.gridviewrow.aspx –

+0

你需要什麼改變是RowIndex:而不是e.Row.RowIndex + 1,離開'+1' –

0

GridView控件不支持本地分組。

+0

我不想自動將它分組,我試圖手動將它分組。 – HOY