Binding datagridview to a multiple table linq-to-sql query

后端 未结 1 955
悲哀的现实
悲哀的现实 2021-01-15 18:45

I\'m binding the results of a linq-to-sql query to a datagridview. This works fine if I\'m just selecting from a single database table. However, if it\'s a join query where

相关标签:
1条回答
  • 2021-01-15 19:16

    What I've done is create a display class, taking the linq to sql table objects as arguments to the constructor and then just wrapping the properties I wanted to display. For example, the following class where I wanted to allow edits to the street and city, but also display the application number and status:

    public class AddressDisplay
    {
        private Retailer _retailer;
        private BusinessAddress _address;
    
        public AddressDisplay(Retailer retailer, BusinessAddress address)
        {
            _retailer = retailer;
            _address = address;
        }
    
        public string ApplicationNumber
        {
            get { return _retailer.ApplicationNumber; }
        }
        public string Status
        {
            get { return _retailer.Status; }
        } 
        public string Street
        {
            get { return _address.Street1; }
            set { _address.Street1 = value; }
        }
        public string City
        {
            get { return _address.City; }
            set { _address.City = value; }
        }
    }
    

    and then return instances of AddressDisplay to bind to the DataGridView:

    var addresses = from a in _context.BusinessAddresses
                    join r in _context.Retailers on a.ApplicationNumber equals r.ApplicationNumber
                    where a.City == city
                    select new AddressDisplay(r, a);
    

    HTH

    0 讨论(0)
提交回复
热议问题