如何在没有数据的情况下隐藏Gridview的列
本文关键字:情况下 隐藏 Gridview 的列 数据 | 更新日期: 2023-09-27 17:50:32
如果没有数据,我想动态隐藏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;
}
}
}
此代码不起作用,它仍在显示列请帮助
您不能这样做。这类似于删除程序集中没有人员的特定列。它会把队形搞砸。
GridView
被呈现为一个表(HTML(。如果查看源,则可以看到一个表,其中每个tr
包含10个td
。这意味着每行10列。现在,您希望有条件地隐藏某些行中的一列。
现在,如果你在第10列的任何一行中都没有数据,你可以隐藏它。但RowDataBound不是这样的事件。在GridView1.DataBind()
之后再做。
如果在调用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();
}