将绑定源 DataSource 设置为 Generic IList<>错误

发布于 2024-09-13 21:47:15 字数 8209 浏览 8 评论 0 原文

如果我想将 BindingSource 上的 DataSource 属性设置为 IList<>,是否需要按照以下错误消息所示进行显式转换,还是我做错了什么?

    interface IView
    {
        IList<OrderItems> BindingSource { get; set;}
    }

    public partial class Form1 : Form, IView
    {
        public Form1()
        {
            InitializeComponent();
        }

        private BindingSource _bindingSource;
        public IList<OrderItems> BindingSource
        {
            get { return _bindingSource; }
            set
            {
                _bindingSource.DataSource = value;
                dataGridView1.DataSource = _bindingSource;                
            }
        }


    }

OrderItems.cs

using System;
using System.Collections.Generic;

namespace Ordering.Data
{
    /// <summary>
    /// Order object for NHibernate mapped table 'Order'.
    /// </summary>
    [Serializable]
    public class OrderItems 
    {
        #region Member Variables
        protected int _id;
        protected Customers _customers;
        protected string _ordername;
        protected DateTime _orderdate;
        protected DateTime? _shipdate;
        protected string _shipvia;
        protected string _shipname;
        protected string _shipaddress;
        protected string _shipcity;
        protected string _shipregion;
        protected string _shippostalcode;
        protected string _shipcountry;
        protected string _status;
        protected DateTime? _lastupdate;
        protected IList<Products> _products;

        #endregion
        #region Constructors

        public OrderItems() {}

        public OrderItems(Customers customers, string ordername, DateTime orderdate, DateTime? shipdate, string shipvia, string shipname, string shipaddress, string shipcity, string shipregion, string shippostalcode, string shipcountry, string status, DateTime? lastupdate) 
        {
            this._customers= customers;
            this._ordername= ordername;
            this._orderdate= orderdate;
            this._shipdate= shipdate;
            this._shipvia= shipvia;
            this._shipname= shipname;
            this._shipaddress= shipaddress;
            this._shipcity= shipcity;
            this._shipregion= shipregion;
            this._shippostalcode= shippostalcode;
            this._shipcountry= shipcountry;
            this._status= status;
            this._lastupdate= lastupdate;
        }

        public OrderItems(Customers customers, string ordername, DateTime orderdate)
        {
            this._customers= customers;
            this._ordername= ordername;
            this._orderdate= orderdate;
        }

        #endregion
        #region Public Properties
        public  virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public  virtual Customers Customers
        {
            get { return _customers; }
            set {_customers= value; }
        }
        public  virtual string OrderName
        {
            get { return _ordername; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "OrderName cannot contain more than 255 characters");
                if (value != this._ordername){_ordername= value;}}
        }
        public  virtual DateTime OrderDate
        {
            get { return _orderdate; }
            set {if (value != this._orderdate){_orderdate= value;}}
        }
        public  virtual DateTime? ShipDate
        {
            get { return _shipdate; }
            set {if (value != this._shipdate){_shipdate= value;}}
        }
        public  virtual string ShipVia
        {
            get { return _shipvia; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipVia cannot contain more than 255 characters");
                if (value != this._shipvia){_shipvia= value;}}
        }
        public  virtual string ShipName
        {
            get { return _shipname; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipName cannot contain more than 255 characters");
                if (value != this._shipname){_shipname= value;}}
        }
        public  virtual string ShipAddress
        {
            get { return _shipaddress; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipAddress cannot contain more than 255 characters");
                if (value != this._shipaddress){_shipaddress= value;}}
        }
        public  virtual string ShipCity
        {
            get { return _shipcity; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipCity cannot contain more than 255 characters");
                if (value != this._shipcity){_shipcity= value;}}
        }
        public  virtual string ShipRegion
        {
            get { return _shipregion; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipRegion cannot contain more than 255 characters");
                if (value != this._shipregion){_shipregion= value;}}
        }
        public  virtual string ShipPostalcode
        {
            get { return _shippostalcode; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipPostalcode cannot contain more than 255 characters");
                if (value != this._shippostalcode){_shippostalcode= value;}}
        }
        public  virtual string ShipCountry
        {
            get { return _shipcountry; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipCountry cannot contain more than 255 characters");
                if (value != this._shipcountry){_shipcountry= value;}}
        }
        public  virtual string Status
        {
            get { return _status; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "Status cannot contain more than 255 characters");
                if (value != this._status){_status= value;}}
        }
        public  virtual DateTime? LastUpdate
        {
            get { return _lastupdate; }
            set {if (value != this._lastupdate){_lastupdate= value;}}
        }
        public  virtual IList<Products> Products
        {
            get { return _products; }
            set {_products= value; }
        }


        #endregion

        #region Equals And HashCode Overrides
        /// <summary>
        /// local implementation of Equals based on unique value members
        /// </summary>
        public override bool Equals( object obj )
        {
            if( this == obj ) return true;
            if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
            OrderItems castObj = (OrderItems)obj;
            return ( castObj != null ) &&
            this._id == castObj.Id;
        }
        /// <summary>
        /// local implementation of GetHashCode based on unique value members
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 57;
            hash = 27 * hash * _id.GetHashCode();
            return hash;
        }
        #endregion

    }
}

无法将类型“System.Windows.Forms.BindingSource”隐式转换为“System.Collections.Generic.IList”。存在显式转换(您是否缺少转换?)

If I want to set the DataSource property on a BindingSource to an IList<>, do I need an explicit cast as the following error message says or am I doing something wrong?

    interface IView
    {
        IList<OrderItems> BindingSource { get; set;}
    }

    public partial class Form1 : Form, IView
    {
        public Form1()
        {
            InitializeComponent();
        }

        private BindingSource _bindingSource;
        public IList<OrderItems> BindingSource
        {
            get { return _bindingSource; }
            set
            {
                _bindingSource.DataSource = value;
                dataGridView1.DataSource = _bindingSource;                
            }
        }


    }

OrderItems.cs

using System;
using System.Collections.Generic;

namespace Ordering.Data
{
    /// <summary>
    /// Order object for NHibernate mapped table 'Order'.
    /// </summary>
    [Serializable]
    public class OrderItems 
    {
        #region Member Variables
        protected int _id;
        protected Customers _customers;
        protected string _ordername;
        protected DateTime _orderdate;
        protected DateTime? _shipdate;
        protected string _shipvia;
        protected string _shipname;
        protected string _shipaddress;
        protected string _shipcity;
        protected string _shipregion;
        protected string _shippostalcode;
        protected string _shipcountry;
        protected string _status;
        protected DateTime? _lastupdate;
        protected IList<Products> _products;

        #endregion
        #region Constructors

        public OrderItems() {}

        public OrderItems(Customers customers, string ordername, DateTime orderdate, DateTime? shipdate, string shipvia, string shipname, string shipaddress, string shipcity, string shipregion, string shippostalcode, string shipcountry, string status, DateTime? lastupdate) 
        {
            this._customers= customers;
            this._ordername= ordername;
            this._orderdate= orderdate;
            this._shipdate= shipdate;
            this._shipvia= shipvia;
            this._shipname= shipname;
            this._shipaddress= shipaddress;
            this._shipcity= shipcity;
            this._shipregion= shipregion;
            this._shippostalcode= shippostalcode;
            this._shipcountry= shipcountry;
            this._status= status;
            this._lastupdate= lastupdate;
        }

        public OrderItems(Customers customers, string ordername, DateTime orderdate)
        {
            this._customers= customers;
            this._ordername= ordername;
            this._orderdate= orderdate;
        }

        #endregion
        #region Public Properties
        public  virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public  virtual Customers Customers
        {
            get { return _customers; }
            set {_customers= value; }
        }
        public  virtual string OrderName
        {
            get { return _ordername; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "OrderName cannot contain more than 255 characters");
                if (value != this._ordername){_ordername= value;}}
        }
        public  virtual DateTime OrderDate
        {
            get { return _orderdate; }
            set {if (value != this._orderdate){_orderdate= value;}}
        }
        public  virtual DateTime? ShipDate
        {
            get { return _shipdate; }
            set {if (value != this._shipdate){_shipdate= value;}}
        }
        public  virtual string ShipVia
        {
            get { return _shipvia; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipVia cannot contain more than 255 characters");
                if (value != this._shipvia){_shipvia= value;}}
        }
        public  virtual string ShipName
        {
            get { return _shipname; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipName cannot contain more than 255 characters");
                if (value != this._shipname){_shipname= value;}}
        }
        public  virtual string ShipAddress
        {
            get { return _shipaddress; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipAddress cannot contain more than 255 characters");
                if (value != this._shipaddress){_shipaddress= value;}}
        }
        public  virtual string ShipCity
        {
            get { return _shipcity; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipCity cannot contain more than 255 characters");
                if (value != this._shipcity){_shipcity= value;}}
        }
        public  virtual string ShipRegion
        {
            get { return _shipregion; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipRegion cannot contain more than 255 characters");
                if (value != this._shipregion){_shipregion= value;}}
        }
        public  virtual string ShipPostalcode
        {
            get { return _shippostalcode; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipPostalcode cannot contain more than 255 characters");
                if (value != this._shippostalcode){_shippostalcode= value;}}
        }
        public  virtual string ShipCountry
        {
            get { return _shipcountry; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "ShipCountry cannot contain more than 255 characters");
                if (value != this._shipcountry){_shipcountry= value;}}
        }
        public  virtual string Status
        {
            get { return _status; }
            set {
                if ( value != null && value.Length > 255)
                    throw new ArgumentOutOfRangeException("value", value.ToString(), "Status cannot contain more than 255 characters");
                if (value != this._status){_status= value;}}
        }
        public  virtual DateTime? LastUpdate
        {
            get { return _lastupdate; }
            set {if (value != this._lastupdate){_lastupdate= value;}}
        }
        public  virtual IList<Products> Products
        {
            get { return _products; }
            set {_products= value; }
        }


        #endregion

        #region Equals And HashCode Overrides
        /// <summary>
        /// local implementation of Equals based on unique value members
        /// </summary>
        public override bool Equals( object obj )
        {
            if( this == obj ) return true;
            if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
            OrderItems castObj = (OrderItems)obj;
            return ( castObj != null ) &&
            this._id == castObj.Id;
        }
        /// <summary>
        /// local implementation of GetHashCode based on unique value members
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 57;
            hash = 27 * hash * _id.GetHashCode();
            return hash;
        }
        #endregion

    }
}

Cannot implicitly convert type 'System.Windows.Forms.BindingSource' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-09-20 21:47:15
return _bindingSource.DataSource as IList<OrderItems>;
return _bindingSource.DataSource as IList<OrderItems>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文