在标题后添加 Gridview 行
我正在尝试向 Gridview 添加新的标题行。 该行应出现在原始标题行下方。
据我所知,我有两个事件可供选择:
1.)Gridview_RowDataBound 2.) Gridview_RowCreated
选项 1 不是一个选项,因为网格不会在每次回发时绑定数据。 选项 2 无法按预期工作。 我可以添加该行,但它被添加在 HeaderRow 之前,因为在此事件中 HeaderRow 本身尚未添加...
请协助,谢谢!
代码:(InnerTable属性由自定义gridview公开)
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
For Each c As DataControlField In CType(sender, GridView).Columns
Dim nc As New TableCell
nc.Text = c.AccessibleHeaderText
nc.BackColor = Drawing.Color.Cornsilk
r.Cells.Add(nc)
Next
Dim t As Table = GridView1.InnerTable
t.Controls.Add(r)
End If
End Sub
i'm trying to add a new headerrow to a Gridview. This row should appear below the original headerrow.
As far as I know I have two events to choose from:
1.) Gridview_RowDataBound
2.) Gridview_RowCreated
Option 1 is not an option as the grid is not binding the data on each postback.
Option 2 does not work as expected. I can add the row, but it is added before the HeaderRow because the HeaderRow itself is not added yet in this event...
Please assist, thank you!
Code: (InnerTable property is exposed by custom gridview)
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
For Each c As DataControlField In CType(sender, GridView).Columns
Dim nc As New TableCell
nc.Text = c.AccessibleHeaderText
nc.BackColor = Drawing.Color.Cornsilk
r.Cells.Add(nc)
Next
Dim t As Table = GridView1.InnerTable
t.Controls.Add(r)
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然这是一个自定义的 GridView,为什么不考虑重写 CreateChildControls 方法呢?
即(对不起,C#):
更新
正如 Ropstah 所提到的,上面的代码片段在分页打开时不起作用。 我将代码移至PrepareControlHierarchy,现在它可以很好地处理分页、选择和排序。
Since this is a custom GridView, why don't you consider overriding the CreateChildControls method?
I.e (sorry, C#):
UPDATE
As was mentioned by Ropstah, the sniplet above does not work with pagination on. I moved the code to a PrepareControlHierarchy and now it works gracefully with pagination, selection, and sorting.
干得好,伙计们,我使用了你们的技术对支持 AJAX 的 gridview 进行分组,并且我搜索了很长很长的时间。 干杯。
Nice work guys, I used your technique for grouping my AJAX enabled gridview, and I searched for a long, long time. Cheers.
将行添加到 InnerTable 时尝试此操作:
这是我所做的一个快速基本测试,似乎工作正常:
Try this when you add the row to the InnerTable:
Here's a quick basic test I did, which seems to work OK: