【发布时间】:2014-09-09 08:35:07
【问题描述】:
我正在尝试在 gridview 中显示/隐藏列以便更好地查看。 我需要做的是:
- 在页面加载时隐藏第 8、9 和 10 列
- 单击按钮时显示它们
我使用 RowCreated 事件(下面的代码)成功地将它们隐藏在页面加载中。但截至目前,我还没有找到通过按钮单击再次显示它们的方法。
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
' Add cells
row.Cells.AddRange(columns.ToArray())
e.Row.Cells(8).Visible = False
e.Row.Cells(9).Visible = False
e.Row.Cells(10).Visible = False
e.Row.Cells(11).Visible = False
End Sub
我尝试了以下方法,结果很不幸:
-
将宽度设置为 0px,然后在点击时重置为自动- 因为我的大部分列都是包含按钮或复选框的项目字段,所以这根本不起作用
使用 GridView1.Columns(8).Visible = False - 原因同上
- 使用 e.Row.Cells(8).Visible = True 创建 RowDataBound 事件,但我无法通过按钮单击成功调用此事件。
请指教。提前致谢。
【问题讨论】:
-
同时显示您的 Gridview 的标记 - 这将使我们能够为您的问题提供具体的答案