【问题标题】:GridView DataBindingGridView 数据绑定
【发布时间】:2011-04-01 13:41:10
【问题描述】:


我的 GridView 有问题(我正在使用 Telerick,但我认为 .NET GridView 与这种情况类似)。
我有一个列表,其中包含一些用户定义对象,其中一些属性将显示在 GridView 中。此列表是从 SQL 加载的。
我的问题是我有一个 int 属性,我想用一些字符串在 GridView 中对其进行解析和显示。

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;

被盗当前在 GridView 中显示为 int。那么有没有一种方法可以解析“isStolen”值并用“YES”/“NO”之类的方式替换它,而不使用 foreach 并在绑定后迭代抛出孔 GridView?

【问题讨论】:

    标签: c# data-binding gridview telerik


    【解决方案1】:

    有两个简单的选择:

    1) 向您的对象添加一个属性并在 DataGrid 中引用该属性:

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

    2) 或者将逻辑添加到 DataGrid 中的

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

    【讨论】:

      【解决方案2】:

      我将修改 SQL 语句,使其根据 isStolen 值返回 Yes/No 字符串。

      【讨论】:

      • 我不能这样做,因为字符串需要本地化。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2015-04-29
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多