将复杂列表绑定到datviewource gridview

时间:2011-08-06 13:12:33

标签: c# asp.net data-binding gridview

是否可能将复杂列表与DataSource绑定到一个包含所有成员的GridView?

如:

    public class Car
    {
        private string _make;
        private string _model;
        private int _year;
        private int _speedCollection;

        public Car(string make, string model, int year)
        {
            _make = make;
            _model = model;
            _year = year;
        }

        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }

        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }

        public List<MyClass> SpeedColections 
        {

            get { return _speedCollection; }
            set { _speedCollection = value; }


        }


    }


   public class MyClass
    {
       private int _speed;
       public int Speed
       {
            get { return _speed; }
            set { _speed = value; }
        }
    }

1 个答案:

答案 0 :(得分:2)

是的,有可能。除了SpeedCollections成员之外,它将起作用,除非你指定另一个公共成员,例如,返回Speed的字符串表示(逗号分隔值或类似的东西)

<强>更新

这是一个会返回SpeedCollections的字符串表示形式的成员的示例:

  

警告!潜在的伪代码,我目前无法编译或测试,所以在需要时进行调整

public string SpeedRepresentation
{
    get
    {
        return string.Join(",", 
                           _speedCollection.Select(s => s.Speed().ToString())
                                           .ToArray());
    }
}