【问题标题】:Adding header rows to Gridview向 Gridview 添加标题行
【发布时间】:2013-03-23 19:51:04
【问题描述】:

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

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

【问题讨论】:

  • 您的gridviewListView 中吗?
  • @HosseinNarimaniRad 抱歉我没听懂?
  • 您可以发布您的 XAML 的某些部分吗?

标签: c# gridview datatable datasource dataview


【解决方案1】:

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

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

【讨论】:

  • 这似乎可以稍作修改,它添加了标题,但在行的下方。请问可以修改吗?我不知道 gridviewrow 参数是否有效
  • 您需要更改的是 RowIndex:不要使用 e.Row.RowIndex+1,而是保留“+1”
【解决方案2】:

GridView 控件本身不支持分组。

【讨论】:

  • 我不是要自动分组,我要手动分组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多