[英]How to rename the binding row element with if condition in asp.net data grid
我正在asp.net .net框架4中工作,我有一个数据网格,并且它的列具有值1,2,3等,我想在绑定if条件时重命名它们,例如,如果value为1,则显示YES ,如果value是2,则显示NO,像这样...我知道网格已绑定了事件数据,但是我不知道如何在此事件的if条件的基础上更改单元格的值,请提供帮助。
谢谢Atif
在.aspx
内联使用条件:
<%# ((int)Eval("Property")) == 0 ? "No" : "Yes" %>
在代码隐藏处使用方法来格式化值:
的.aspx
<%# FormatMyValue(Container.DataItem) %>
的.cs
public string FormatMyValue(object value) { return ((MyDataType)value).Property == 0 ? "No" : "Yes"; }
在代码隐藏中使用RowDataBound
事件(虽然不太优雅,但应该可以):
protected void Page_Load(object sender, EventArgs e) { this.GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound); } void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // get date item MyDataType item = (MyDataType)e.Row.DataItem; // Set value in the necessary cell. You need to specify correct cell index. e.Row.Cells[1].Text = item.Property == 0 ? "No" : "Yes"; ; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.