【问题标题】:How to hide column of Gridview when there is no data?没有数据时如何隐藏Gridview的列?
【发布时间】:2015-08-05 05:59:22
【问题描述】:

如果没有数据,我想动态隐藏gridview的列。 有一列,即我想隐藏的附件,但不幸的是编码有问题,但我找不到它 以下是我的代码

<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>

以下是.aspx.cs

 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;     
        }

    }
}

此代码不起作用,它仍在显示该列 请帮忙

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    如果您在调用 DataBind 之前设置了 Visible 属性,那么它还会稍微减少 ViewState,因为这些值永远不会填充到不可见的列中。

    protected void btnSearch_Click(object sender, EventArgs e) {
    // .....
    bool isBlank = true;
    foreach (DataRow dr in dt.Rows) { // for each row in the data table
        if (!dr.item("Attachment") == System.DbNull.Value) { // if we find a non null value
            isBlank = false; // show the column in the gridview
            break; // then stop checking for nulls
        }
    }
    GridView1.Columns(9).Visible = !isBlank;
    GridView1.DataBind();
    }
    

    【讨论】:

      【解决方案2】:

      你不能那样做。它类似于删除人员不存在于程序集中的特定列。它会搞砸阵型。

      GridView 呈现为表格 (HTML)。如果您查看源代码,您可以看到一个表格,其中每个 tr 有 10 个 td。这意味着每行 10 列。现在,您想有条件地隐藏某些行中的一列。

      现在,如果您在第 10 列的任何行中都没有数据,您可以将其隐藏。但是 RowDataBound 不是针对该事件的。在您的GridView1.DataBind() 之后执行此操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-30
        • 2013-10-15
        • 2011-11-25
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        相关资源
        最近更新 更多