我希望能用象DataGrid.DataSource = ds那样进行数据绑定。

解决方案 »

  1.   

    DataGrid可以绑定到实现了相关接口(IList等)的数据源。我现在有一个自定义的类(见问题的描述),怎样把他绑定到DataGrid(用.Net本身的数据绑定机制,就象是DataSet绑定到DataGrid一样)
      

  2.   

    你在
    class Table
    {
    ArrayList m_Item = new ArrayList(); // 放置Row对象
    }
    中一定会用到对class Item、class Row的引用;
    所以你可以在class Table中先定义:private DataTable dataTable;
    然后定义一个方法:
    public DataTable GetDataTable()
    {
     return this.dataTable;
    }
    在绑定dataGrid时只要:
    Table tableClass = new Table();
    DataTable table = tableClass.GetDataTable();
    DataGrid.DataSource = table ;
    就可以了
      

  3.   

    我要绑定的是自定义类,而不是.Net的DataTable类。
      

  4.   

    如果绑定到没有实现 IBindingList 接口的数据源(如 ArrayList 对象),则在更新数据源时,将不会更新绑定控件的数据。例如,如果将组合框绑定到 ArrayList 对象并将数据添加到 ArrayList 中,则这些新项将不会出现在组合框中。但是,您可以通过调用控件绑定到的 BindingContext 类的实例上的 SuspendBinding 和 ResumeBinding 方法强制更新组合框。实现IBindingList接口,就可以绑定到DataSource上了,请看例子:
     
    public class CustomersList :  CollectionBase, IBindingList
    {    private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);
        private ListChangedEventHandler onListChanged;    public void LoadCustomers() 
        {
            IList l = (IList)this;
            l.Add(ReadCustomer1());
            l.Add(ReadCustomer2());
            OnListChanged(resetEvent);
        }    public Customer this[int index] 
        {
            get 
            {
                return (Customer)(List[index]);
            }
            set 
            {
                List[index] = value;
            }
        }    public int Add (Customer value) 
        {
            return List.Add(value);
        }    public Customer AddNew() 
        {
            return (Customer)((IBindingList)this).AddNew();
        }    public void Remove (Customer value) 
        {
            List.Remove(value);
        }    
        protected virtual void OnListChanged(ListChangedEventArgs ev) 
        {
            if (onListChanged != null) 
            {
                onListChanged(this, ev);
            }
        }
            protected override void OnClear() 
        {
            foreach (Customer c in List) 
            {
                c.Parent = null;
            }
        }    protected override void OnClearComplete() 
        {
            OnListChanged(resetEvent);
        }    protected override void OnInsertComplete(int index, object value) 
        {
            Customer c = (Customer)value;
            c.Parent = this;
            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
        }    protected override void OnRemoveComplete(int index, object value) 
        {
            Customer c = (Customer)value;
            c.Parent = this;
            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
        }    protected override void OnSetComplete(int index, object oldValue, object newValue) 
        {
            if (oldValue != newValue) 
            {            Customer oldcust = (Customer)oldValue;
                Customer newcust = (Customer)newValue;
                
                oldcust.Parent = null;
                newcust.Parent = this;
                
                
                OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
            }
        }
        
        // Called by Customer when it changes.
        internal void CustomerChanged(Customer cust) 
        {
            
            int index = List.IndexOf(cust);
            
            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
        }
            // Implements IBindingList.
        bool IBindingList.AllowEdit 
        { 
            get { return true ; }
        }    bool IBindingList.AllowNew 
        { 
            get { return true ; }
        }    bool IBindingList.AllowRemove 
        { 
            get { return true ; }
        }    bool IBindingList.SupportsChangeNotification 
        { 
            get { return true ; }
        }
        
        bool IBindingList.SupportsSearching 
        { 
            get { return false ; }
        }    bool IBindingList.SupportsSorting 
        { 
            get { return false ; }
        }
        // Events.
        public event ListChangedEventHandler ListChanged 
        {
            add 
            {
                onListChanged += value;
            }
            remove 
            {
                onListChanged -= value;
            }
        }    // Methods.
        object IBindingList.AddNew() 
        {
            Customer c = new Customer(this.Count.ToString());
            List.Add(c);
            return c;
        }
        // Unsupported properties.
        bool IBindingList.IsSorted 
        { 
            get { throw new NotSupportedException(); }
        }    ListSortDirection IBindingList.SortDirection 
        { 
            get { throw new NotSupportedException(); }
        }
        PropertyDescriptor IBindingList.SortProperty 
        { 
            get { throw new NotSupportedException(); }
        }
        // Unsupported Methods.
        void IBindingList.AddIndex(PropertyDescriptor property) 
        {
            throw new NotSupportedException(); 
        }    void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) 
        {
            throw new NotSupportedException(); 
        }    int IBindingList.Find(PropertyDescriptor property, object key) 
        {
            throw new NotSupportedException(); 
        }    void IBindingList.RemoveIndex(PropertyDescriptor property) 
        {
            throw new NotSupportedException(); 
        }    void IBindingList.RemoveSort() 
        {
            throw new NotSupportedException(); 
        }    // Worker functions to populate the list with data.
        private static Customer ReadCustomer1() 
        {
            Customer cust = new Customer("536-45-1245");
            cust.FirstName = "Jo";
            cust.LastName = "Brown";
            return cust;
        }
        
        private static Customer ReadCustomer2() 
        {
            Customer cust = new Customer("246-12-5645");
            cust.FirstName = "Robert";
            cust.LastName = "Brown";
            return cust;
        }