请教:如何进行自定义的数据绑定? 我希望能用象DataGrid.DataSource = ds那样进行数据绑定。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataGrid可以绑定到实现了相关接口(IList等)的数据源。我现在有一个自定义的类(见问题的描述),怎样把他绑定到DataGrid(用.Net本身的数据绑定机制,就象是DataSet绑定到DataGrid一样) 你在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 ;就可以了 我要绑定的是自定义类,而不是.Net的DataTable类。 如果绑定到没有实现 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; } 如何将数组中的字段插入到oracle数据库相应的字段中去 为什么同一个DLL,c#与MFC的调用结果不一致呢? 将数值插入文本文件 怎样把xml文件转换为 byte[] 在线等,急!!! C/S程序如何获取本机某站点下,某虚拟目录的物理路径? 請教C#與LOTUS NOTES如何連接? 问一个ado.net的问题 在显示MessageBox的同时加一个自定义的音效? 怎样才能完全退出程序 讨论.net和C#的新天地:“xml中华网”.net论坛! 关于水晶报表(crystalreport)读写数据的问题! 如何取得客户端信息?如 Ip,时间等?
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 ;
就可以了
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;
}