Search code examples
c#asp.netdata-bindinggridview

Bind complex list to datasource a gridview


whether possible Bind complex list to DataSource a GridView with all members?

such as:

    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; }
        }
    }

Solution

  • Yes, it is possible. And it will work, except for the SpeedCollections member, unless you specify another public member that would return, say, a string representation of Speed (comma separated values or something of this sort)

    Update

    This is an example of a member that would return a string representation of SpeedCollections:

    Warning! Potential pseudo-code ahead, I cannot currently compile or test, so make your adjustments when needed

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