0

我的网格视图类似于:

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

我可以通过修改我绑定为数据源的数据表来做到这一点吗?还是有更简单的方法?

4

2 回答 2

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

代码按原样提供,未经测试。

于 2013-04-02T13:59:43.353 回答
0

GridView 控件本身不支持分组。

于 2013-04-02T13:35:20.607 回答