如果没有数据,我想动态隐藏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;
}
}
}
此代码无法正常显示该列 请帮忙
答案 0 :(得分:0)
你做不到。它类似于删除人们不在装配中的特定列。它会搞砸阵型。
GridView
呈现为表格(HTML)。如果您查看来源,则每个td
都会看到一个包含10 tr
的表格。这意味着每行10列。现在,您希望有条件地隐藏某些行中的一列。
现在,如果第10列的任何行中没有数据,则可以隐藏它。但是RowDataBound不是那个事件。在GridView1.DataBind()
之后执行此操作。
答案 1 :(得分:0)
If you set the Visible property before calling DataBind, then it also reduces ViewState slightly as the values are never populated into invisible columns.
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();
}