【问题标题】:How to readd removed columns in gridview如何在gridview中重新添加删除列
【发布时间】:2015-07-23 05:01:14
【问题描述】:

我只是对如何在网格视图中再次添加已删除的列有疑问。我有一个应用程序,例如,当您选择 1 时,其他列被隐藏,当您选择 2 时,隐藏列应该出现,我尝试使其可见 true 和 false,但是当我将其数据导出到 excel 所有其他隐藏列时出现另一个问题也被保存在excel中,因为这些列只是隐藏的,所以我开始搜索如何再次添加已删除的列,希望你能帮助我。这就是我删除列的方式。

Public Sub RemoveGridColumn(ByRef Data As GridView, ByVal colName As String)
    For x As Integer = 0 To Data.Columns.Count - 1
        If Data.Columns(x).HeaderText = colName Then
            Data.Columns.RemoveAt(x)
            Exit For
        End If
    Next
End Sub

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    在隐藏不想导出到 Excel 的列之前,您需要调用 GridView 控件的 DataBind 方法,类似于以下内容:

    Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As EventArgs)
        '' Set response settings here (header, content type, etc.)
    
        '' Turn paging off so you get all the rows of the grid
        GridView1.AllowPaging = False
    
        '' Make sure to bind before you hide anything
        GridView1.DataBind()
    
        '' Put logic here to hide columns that need to be hidden
        '' Possible from Session/ViewState, etc.
    
    
        '' Render out to response
    
    End Sub
    

    如需完整的代码示例(在 VB.NET 和 C# 中),请查看ASP.NET GridView Export to Excel - Hide Columns

    【讨论】:

      【解决方案2】:

      请考虑使用 CSS 设置打印的可见性:

      @media print {
      .noPrint {
          display:none;
        }
      }
      

      数据绑定事件期间的代码部分应该是这样的:

      Data.Columns(x).ItemStyle.CssClass = "noPrint"
      

      通过这种方式,您可以使用 javascript 来更改打印可见性:

      function hideColumn()
      {
          col_num = document.getElementById("ColumnToChange").value;
          rows = document.getElementById("GridViewId").rows;
          for(i=0;i<maxrow;i++) {
             rows[i].cells[col_num].style.display="none";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-14
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2011-06-06
        • 1970-01-01
        相关资源
        最近更新 更多