隐藏Gridview列时为null

时间:2014-11-17 08:57:48

标签: asp.net gridview

我的前端有一个gridview,显示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列。如何实现?

3 个答案:

答案 0 :(得分:1)

是的,您可以动态创建boundfield值 根据您的要求从代码背后 点击此链接 add boundField to gridview in codebehind file C#

答案 1 :(得分: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
                }

            }
    }

答案 2 :(得分:0)

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