【问题标题】:Hide Gridview column when null为空时隐藏 Gridview 列
【发布时间】:2014-11-17 08:57:48
【问题描述】:

我在前端有一个显示 4-5 列的网格视图。当值为 NULL 时需要隐藏其中一列。

<asp:GridView ID="gvProducts" runat="server" BackColor="#DEBA84"
                BorderColor="#DEBA84"
                BorderStyle="None" BorderWidth="1px" CellPadding="3"
                CellSpacing="2" AutoGenerateColumns="False" AllowPaging="True" PageSize="100" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="CategoryId" SortExpression="CategoryId" HeaderText="CategoryId" Visible="false" />
                    <asp:BoundField DataField="MerchantName" HeaderText="MerchantName" SortExpression="MerchantName" />
                    <asp:BoundField DataField="StoreName" HeaderText="StoreName" SortExpression="StoreName" />
                    <asp:BoundField DataField="StoreAddress" HeaderText="StoreAddress" SortExpression="StoreAddress" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                </Columns>
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True"
                    ForeColor="White" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                    ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
            </asp:GridView>

如果我想从代码隐藏中隐藏 StoreName 列。如何实现?

【问题讨论】:

  • 如何将数据绑定到gridview?如果您可以管理您选择的数据,那么当StoreName为空时只选择不为空的列,并设置AutoGenerateColumns="True"。
  • 一列可以包含多个值,不一定都是null。
  • 所有值或其中一个值为 NULL ???
  • @Ganesh_Devlekar:对于值之一
  • @RahulSutar 我发布了答案试试

标签: asp.net gridview


【解决方案1】:

你可以使用RowDataBound事件GridView

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            {
               string val = e.Row.Cells[0].ToString();  //check first cell value
               if(string.IsNullorEmpty(val) )             
                {
                gvSearchEngine.Columns[0].Visible = false;     //Hides First Column
                }

            }
    }

【讨论】:

  • 用你的代码试过了,我仍然可以看到该列。请参阅链接。 StoreName 列仍为空白且可见。 imagesup.net/?di=1214162190672
  • 尝试调试您将了解的代码
  • 我调试了代码,它没有进入 IF 条件
  • 那么 string.IsNullorEmpty(val) 应该返回 true
【解决方案2】:

是的,您可以动态创建绑定字段值 根据您的要求从后面的代码 按照这个链接 add boundField to gridview in codebehind file C#

【讨论】:

    【解决方案3】:

    试试 e.Row.Cells[0].Controls[0].Visible = false;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2011-11-25
      • 2014-08-24
      • 1970-01-01
      相关资源
      最近更新 更多