2011-07-20 44 views
1

我一直在写网格视图的扩展,通过监视RowDataBoundEvent中所需项目的更改时将分组行添加到GridView。到目前为止,它看起来有点像这样:将行添加到GridView Swallows Paging事件

protected override void OnRowDataBound(GridViewRowEventArgs args) 
{ 
    if (args.Row.RowType == DataControlRowType.DataRow) 
    { 
     object dataitem = args.Row.DataItem; 

     string currentValue = dataitem.GetType().GetProperty(GroupingColumn).GetValue(dataitem, null).ToString(); 

     if (string.IsNullOrEmpty(previousValue) || currentValue != previousValue) 
     { 
      int currentRowIndex = args.Row.RowIndex + rowsAdded; 
      int colspan = this.Columns.Count - 1; 
      Label label = new Label { Text = currentValue, CssClass = "rowLabel" }; 
      TableCell cell = new TableCell { ColumnSpan = colspan, CssClass = "groupHeader" }; 
      GridViewRow newRow = new GridViewRow(currentRowIndex, currentRowIndex, DataControlRowType.DataRow, DataControlRowState.Normal); 

      cell.Controls.Add(label);         
      newRow.Cells.Add(cell); 
      ((Table)args.Row.Parent).Controls.AddAt(currentRowIndex, newRow); 

      previousValue = currentValue; 
      rowsAdded++; 
     } 
    } 

    base.OnRowDataBound(args); 
} 

的问题是,当我点击分页按钮,在分页不会触发事件,并且额外添加的行成为空行。页面回发,并贯穿页面加载等,但从来没有通过分页事件。我也在此扩展中内置了自定义分页,并且我的分页按钮的单击事件也不会触发。直到第二次点击分页按钮时才发生分页。

我认为这是因为在rowdatabound事件之前构造了分页行,在此期间创建了分页按钮并分配了它们的事件处理程序。但是,向基础表添加行将删除这些处理程序。任何人有任何想法如何确保自定义分页按钮触发?

编辑1:为了澄清,添加到数据源,然后重新绑定将不起作用,因为没有额外的数据要添加。我试图实现的效果如下所示:

| Group 1 |

|第1项|第2项|第3项|

|第4项|第5项|第6项|

| Group 2 |

|第7项|第8项|第9项|

+1

它看起来像你的时候,你上你的Page_Load中的火灾,你重新绑定网格以“旧”的数据源,并放弃您的值添加行到电网而不是数据源添加。 – Dave

+0

我不想将行添加到数据源,因为没有额外的数据要添加。相反,额外的行充当组头。 –

回答

1

找到了解决类似问题的答案。诀窍是在PrepareControlHierarchy事件中添加行。此外,请确保您的行类型是“标题”,否则可能会出现一些使用自定义分页的令人讨厌的副作用。

Add Gridview Row AFTER Header