Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to hide the column of gridview dynamically when there is not data. There is column i.e. Attachment which i want to hide but unfortunately there is something wrong in coding but i am not able to find it.
Following is my code
C#
<asp:GridView ID="GridView1" CssClass="attengrid" runat="server" Width="100%" AutoGenerateColumns="false"
                        ShowHeader="true" onrowdatabound="GridView1_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="EmpName" HeaderText="Emp.Name"></asp:BoundField>
                            <asp:BoundField DataField="DOB" HeaderText="DOB"></asp:BoundField>
                            <asp:BoundField DataField="Qualification" HeaderText="Designation"></asp:BoundField>
                            <asp:BoundField DataField="HomePlace" HeaderText="Home Town"></asp:BoundField>
                            <asp:BoundField DataField="DOJInGovrService" HeaderText="DOJ In Gov.Service"></asp:BoundField>
                            <asp:BoundField DataField="DOJInSamvarg" HeaderText="DOJ In Samvarg"></asp:BoundField>
                            <asp:BoundField DataField="DOJInCurrentOff" HeaderText="DOJ In Current Off."></asp:BoundField>
                            <asp:BoundField DataField="CurrentOfficePlace" HeaderText="Current Office"></asp:BoundField>
                            <asp:BoundField DataField="class" HeaderText="Category"></asp:BoundField>
                            <asp:BoundField DataField="Attachment" HeaderText="Attachment"></asp:BoundField>
                        </Columns>
                    </asp:GridView>


Following is .aspx.cs
C#
protected void btnSearch_Click(object sender, EventArgs e)
{

    dbAccess.execute("select ED.class,ED.CurrentOfficePlace,ED.DOB,ED.DOJInCurrentOff,ED.DOJInGovrService,ED.DOJInSamvarg,ED.EmpName,ED.HomePlace,ED.Qualification, ED.Attachment from tbl_EmplyeesBiodata ED where ED.CurrentOfficePlace='" + ddlCurrentPlacePosting.SelectedItem.Text + "'", DBAccess.SQLType.IS_QUERY);
    DataTable dt = dbAccess.records1();
    if (dt.Rows.Count > 0)
    {
        Label8.Text = dt.Rows.Count.ToString();
        GridView1.DataSource = dt;
        GridView1.DataBind();
        lblmsg.Style.Add("display", "block");
        lblmsg.Attributes.Add("class", "success");
        lblmsg.InnerHtml = closediv + "Case Found";
        tdnotice.Style.Add("display", "block");

    }
    else
    {

        lblmsg.Style.Add("display", "block");
        lblmsg.Attributes.Add("class", "error");
        lblmsg.InnerHtml = closediv + "No Case Found";
        tdnotice.Style.Add("display", "none");

    }
}


 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string val = e.Row.Cells[9].ToString();  
        if (string.IsNullOrEmpty(val))
        {
            GridView1.Columns[9].Visible = false;     
        }

    }
}


Please help
Posted
Updated 22-May-15 20:50pm
v2

1 solution

Hi,
Even if one row has value for attachment, the Attachment column has to be visible right?
In that case, this solution should help you.

Make the Attachment column hidden by default and if any of the row has value for attachment, then make Attachment column visible.

Add these two classes to CSS file and reference that to the page.
C#
.hideGridColumn
    {
        display:none;
    }

.showGridColumn
    {
        display:inline;
    }


Change the column definition like below:
<asp:BoundField DataField="Attachment" ItemStyle-CssClass="hideGridColumn" HeaderStyle-CssClass="hideGridColumn" HeaderText="Attachment"/>
In the GridView1_RowDataBound, check for existence of value and make the column visible if value is present. Assuming the cell index for attachment column as 10 based on the code provided in question.
C#
protected void gvChangeColor_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        bool makeVisible = false;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!string.IsNullOrEmpty(e.Row.Cells[10].Text) && e.Row.Cells[10].Text != " ")
            {
                makeVisible = true;
            }
        }
        if (makeVisible)
        {
            gvChangeColor.HeaderRow.Cells[10].CssClass = "showGridColumn";
            gvChangeColor.Columns[10].ItemStyle.CssClass = "showGridColumn";
        }
    }
    catch (Exception ex)
    {
        string msg = ex.Message;
    }
}
 
Share this answer
 
v2
Comments
Member 11589183 23-May-15 4:45am    
thanks Mathi Mani but it is also not working even when it has data it is not showing
Mathi Mani 23-May-15 19:27pm    
Hi, updated the solution. Try now and see if it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900