开发者

GridView DataBinding

I have a problem with a GridView(I'm using Telerick but i think the .NET GridView is similar for this situation).

I have a List that contains some user define object with some properties that will be displayed in a GridView. This list is loaded from SQL.

My problem is that i have an int property that i want to be parsed and displayed in GridView with some strings.

public class Vehicles
{
    private int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string vehName;
    public string VehName
    {
        get { return vehName; }
        set { vehName = value; }
    }

    private int gpsInterval;
    public int GpsInterval
    {
        get { return gpsInterval; }
        set { gpsInterval = value; }
    }

    private int isStolen;
    public int IsStolen
    {
        get { 开发者_开发百科return isStolen; }
        set { isStolen = value; }
    } 
... 

}

...
List<Vehicles> vehs = DBveh.getAllVehicles();
GridViewUnitsList.DataSource = vehs;

Is stolen is curently displayed as an int in the GridView. So is there a method to parse "isStolen" value and replace it with somenting like "YES"/"NO" without using a foreach and iterating throw the hole GridView after the binding?


There are 2 easy options:

1) Add a property to your object and reference that property in the DataGrid:

public string IsStolenStr
{
    get { return isStolen == 1? "Yes" : "No"; }
}

2) Or add the logic to a <asp:template> column in the DataGrid:

<%# Eval("IsStolen") == 1 ? "Yes" : "No" %>


I would modify the SQL statement so that it returned the Yes/No string based on the isStolen value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜