【问题标题】:How can I hide columns in GridView?如何在 GridView 中隐藏列?
【发布时间】:2014-12-16 01:57:38
【问题描述】:

我有一个 GridView,如下图所示,我想在其中显示 SQL Server 中存储过程的结果。根据其参数的输入,存储过程返回具有不同列数的不同结果。但是它的前两列始终是 measId 和 valSeq,它们在 GridView 中被视为 DataKeyNames。我想显示存储过程中的所有可用列,但 measId 和 valSeq。相反,我想显示 2 TemplateField。

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
     DataSourceID="SqlDataSource1" Width="100%"  BackColor="#DEBA84" 
     BorderColor="#DEBA84" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
     CellSpacing="2" DataKeyNames="measId,valSeq" 
     onrowupdating="GridView_RowUpdating" Visible="False" 
     EmptyDataText="No data!" EnableModelValidation="True" 
     onrowdatabound="GridView1_RowDataBound">
     <Columns>
        <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox ID="cbCheckAll" runat="server" OnClick="javascript:selectAll(this)" />
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="Checkbox1" runat="server" />
        </ItemTemplate>
        <ItemStyle Width="25px" />
        </asp:TemplateField>
        <asp:CommandField HeaderText="Action" ShowEditButton="True" />
     </Columns>
     <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
     <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" 
         Font-Names="Verdana" Font-Size="Small" HorizontalAlign="Left" />
     <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
     <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" Font-Names="Verdana" 
         Font-Size="X-Small" HorizontalAlign="Left" />
     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>

我可以成功声明一个 SqlDataSource 以从存储过程中检索结果并将其与 GridView 绑定。 GridView 显示 TemplateFiled 以及从存储过程中检索到的所有列。现在我想隐藏 measId 和 valSeq 列。但我找不到任何方法来做到这一点。似乎我无法访问自动绑定到 GridView 的字段。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我终于找到了答案here。但是,由于我想隐藏第三列,我不得不添加一个条件语句。因此,如果您想隐藏第 3 列,解决方案如下:

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.Cells.Count > 2)
                e.Row.Cells[2].Visible = false;
        }
    

    【讨论】:

      【解决方案2】:
      <style type="text/css">
           .hidden
           {
               display:none;
           }
      </style>
      
              <Columns>
                  <asp:BoundField HeaderStyle-CssClass="hidden" DataField="TemplateID" HeaderText="Template ID" 
                      ReadOnly="True" SortExpression="TemplateID" >
                         <ItemStyle CssClass="hidden"/>
                  </asp:BoundField>
                  <asp:BoundField DataField="TemplateName" HeaderText="Template Name" 
                      SortExpression="TemplateName" />
              </Columns>
      

      【讨论】:

      • 它不适用于已经显示的列。它只是添加了一个新的隐藏列!
      【解决方案3】:

      使用 GridView 的 RowDataBound 事件将列设置为不可见。

      protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          e.Row.Cells[index].Visible = false;
      }
      

      0r

      this.myGridview.Columns[0].Visible = false;
      

      这里的0是列索引

      Hiding column in GridView

      protected void GridView_PreRender(object sender, EventArgs e)
      {
          foreach (DataControlField column in GridView1.Columns)
              if (column.HeaderText == "XXXXXXX")
                  column.Visible = false;
      }
      

      【讨论】:

      • 好的。我使用了 2 行 e.Row.Cells[x].Visible = false;其中 x = 2 和 x =3 隐藏我想要的列(前两列是自定义模板字段)。但它不会隐藏标题。如何隐藏标题?顺便说一句,myGridview.Columns 只解决了前两个 TemplateFields!
      • 还是不行。请注意,我使用的是自动生成的字段,这些字段未收集在 GridView1.Columns 中。
      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 2010-09-17
      • 2015-10-29
      • 2015-09-16
      • 2014-12-24
      • 1970-01-01
      • 2011-08-26
      • 2011-11-25
      相关资源
      最近更新 更多