【问题标题】:Dynamically show/hide columns in Gridview in ASP.NET在 ASP.NET 的 Gridview 中动态显示/隐藏列
【发布时间】:2014-09-09 08:35:07
【问题描述】:

我正在尝试在 gridview 中显示/隐藏列以便更好地查看。 我需要做的是:

  1. 在页面加载时隐藏第 8、9 和 10 列
  2. 单击按钮时显示它们

我使用 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

我尝试了以下方法,结果很不幸:

  1. 将宽度设置为 0px,然后在点击时重置为自动- 因为我的大部分列都是包含按钮或复选框的项目字段,所以这根本不起作用

  2. 使用 GridView1.Columns(8).Visible = False - 原因同上

  3. 使用 e.Row.Cells(8).Visible = True 创建 RowDataBound 事件,但我无法通过按钮单击成功调用此事件。

请指教。提前致谢。

【问题讨论】:

  • 同时显示您的 Gridview 的标记 - 这将使我们能够为您的问题提供具体的答案

标签: asp.net vb.net gridview


【解决方案1】:

DataGridView1.Columns(n).Visible = False ' 在 vb.net 工作

其中n代表列索引

【讨论】:

  • 谢谢!我还了解到将 gridview 放在更新面板中非常重要,否则它将无法工作。 :)
  • 您可以在使用更新面板时将数据加载到网格或在网格中维护数据,而无需回发页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 2013-07-03
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多