How to rename the binding row element with if condition in asp.net data grid
I am working in asp.net .net framework 4, i have a data grid and it has a column having values 1,2,3 etc, i want to rename them during binding with if condition, for example if value is 1 display YES, if value is 2, display NO, like that... I know that grid has an event data bound, but i dont know how to change the valu开发者_StackOverflowe of cell on the bases of if condition in this event, please help.
Thanks Atif
Use condition inline in
.aspx
:<%# ((int)Eval("Property")) == 0 ? "No" : "Yes" %>
Use method at codebehind to format value:
.aspx
<%# FormatMyValue(Container.DataItem) %>
.cs
public string FormatMyValue(object value) { return ((MyDataType)value).Property == 0 ? "No" : "Yes"; }
Using
RowDataBound
event in codebehind (not elegant, but should work):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"; ; } }
精彩评论