C# 当没有数据时,如何隐藏Gridview的列?
如果没有数据,我想动态隐藏gridview的列。 有一列,即附件,我想隐藏,但不幸的是,有一些编码错误,但我无法找到它 下面是我的代码C# 当没有数据时,如何隐藏Gridview的列?,c#,asp.net,gridview,C#,Asp.net,Gridview,如果没有数据,我想动态隐藏gridview的列。 有一列,即附件,我想隐藏,但不幸的是,有一些编码错误,但我无法找到它 下面是我的代码 <asp:GridView ID="GridView1" CssClass="attengrid" runat="server" Width="100%" AutoGenerateColumns="false" ShowHeader="true" onrowdatabound="GridView1_RowDa
<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>
此代码不起作用,但仍显示列
请帮忙,你不能那样做。这类似于删除程序集中没有人员的特定列。它会把队形搞得一团糟
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();
}
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();
}