没有数据时如何隐藏Gridview的列?

编程入门 行业动态 更新时间:2024-10-28 04:16:30
本文介绍了没有数据时如何隐藏Gridview的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我想在没有数据的情况下动态隐藏gridview列。有一列,即我想隐藏的附件,但不幸的是编码有问题,但我无法找到它。 以下是我的代码

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

<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

Following is .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; } } }

请帮助

Please help

推荐答案

即使一行有附件值,附件列也必须可见吗? 在这种情况下,此解决方案应该可以帮助您。 默认情况下隐藏附件列,如果任何行具有附件值,则使附件列可见。 将这两个类添加到CSS文件并将其引用到页面。 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. .hideGridColumn { display:none; } .showGridColumn { display:inline; }

更改列定义如下:

Change the column definition like below:

<asp:BoundField DataField="Attachment" ItemStyle-CssClass="hideGridColumn" HeaderStyle-CssClass="hideGridColumn" HeaderText="Attachment"/>

在GridView1_RowDataBound中,检查是否存在值,如果存在值,则使列可见。根据相关代码,假设附件列的单元索引为10.

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.

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

更多推荐

没有数据时如何隐藏Gridview的列?

本文发布于:2023-10-30 22:16:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!