0

I have a gridview in the front end showing 4-5 columns. Need to hide one of the column when the values are 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>

If I want to hide StoreName column from code-behind. How to achieve that ?

Rahul Sutar
  • 260
  • 9
  • 36

3 Answers3

1

Yes, you can dynamically create the boundfield values from code behind based on your requirment follow this link add boundField to gridview in codebehind file C#

Community
  • 1
  • 1
dazzling kumar
  • 584
  • 1
  • 8
  • 17
1

You can Use RowDataBound Event of 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
                }

            }
    }
Dgan
  • 10,077
  • 1
  • 29
  • 51
0

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