asp.net – GridView的RowDataBound函数
发布时间:2020-12-16 00:10:09 所属栏目:asp.Net 来源:网络整理
导读:我有一个包含3个字段的DataTable:ACount,BCount和DCount.如果ACount 0然后我需要在GridView的一列中显示'S'.如果ACount 0然后我必须在该列中显示“D”(在标签中).与BCount和DCount相同.如何在RowDataBound函数中执行此条件检查? 解决方法 GridView OnRowDa
我有一个包含3个字段的DataTable:ACount,BCount和DCount.如果ACount< 0然后我需要在GridView的一列中显示'S'.如果ACount> 0然后我必须在该列中显示“D”(在标签中).与BCount和DCount相同.如何在RowDataBound函数中执行此条件检查?
解决方法
GridView
OnRowDataBound 活动是您的朋友:
<asp:gridview id="myGrid" onrowdatabound="MyGrid_RowDataBound" runat="server"> <columns> <asp:boundfield headertext="ACount" datafield="ACount" /> <asp:boundfield headertext="BCount" datafield="BCount" /> <asp:boundfield headertext="DCount" datafield="DCount" /> <asp:templatefield headertext="Status"> <itemtemplate> <asp:label id="aCount" runat="server" /> <asp:label id="bCount" runat="server" /> <asp:label id="dCount" runat="server" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> // Put this in your code behind or <script runat="server"> block protected void MyGrid_RowDataBound(object sender,GridViewRowEventArgs e) { if(e.Row.RowType != DataControlRowType.DataRow) { return; } Label a = (Label)e.Row.FindControl("aCount"); Label b = (Label)e.Row.FindControl("bCount"); Label d = (Label)e.Row.FindControl("dCount"); int ac = (int) ((DataRowView) e.Row.DataItem)["ACount"]; int bc = (int) ((DataRowView) e.Row.DataItem)["BCount"]; int dc = (int) ((DataRowView) e.Row.DataItem)["DCount"]; a.Text = ac < 0 ? "S" : "D"; b.Text = bc < 0 ? "S" : "D"; d.Text = dc < 0 ? "S" : "D"; } 我不确定你想要’S’和’D字符呈现的位置,但你应该能够重新设置以满足你的需求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – 在ASP.net Core中使用BeginCollectionItem
- asp.net-web-api – 无法为Web API OData端点解析MapODataR
- asp.net – Gridview中的TimeSpan DataFormatString
- asp.net-mvc – 城堡PerRequestLifestyle无法识别
- 从ASP.NET IIS运行时,Powershell启动作业,等待作业,主机线程
- asp.net – 配置IIS以返回404以进行目录浏览尝试
- asp.net-mvc – ASP.Net MVC:控制器中的访问验证错误消息?
- ASP.Net的Web TWAIN扫描解决方案?
- asp.net-mvc – ASP.NET MVC3 IIS7.5:Cache-Control maxag
- asp.net-mvc – ASP.NET MVC 4移动显示模式停止工作