我用IList list=new ArrayList(),然后多次往list加入含有属性(p1,p2)的对象。
现我想用dataGrid一行行的显示所有对象的属性列表,一个对象一条记录对象1.p1 对象1.p2
对象2.p1 对象2.p2
对象3.p1 对象3.p2我用dataGrid.dataSource=list;dataGrid.DataBind();怎么得不到我要的效果
请问各位大侠如何做?谢谢
现我想用dataGrid一行行的显示所有对象的属性列表,一个对象一条记录对象1.p1 对象1.p2
对象2.p1 对象2.p2
对象3.p1 对象3.p2我用dataGrid.dataSource=list;dataGrid.DataBind();怎么得不到我要的效果
请问各位大侠如何做?谢谢
public class 对象
public 对象ID as int32
public 对象名 as string
end classDim Atemp as new arraylist
Dim P as 对象P =new 对象
p.对象ID=1
p.对象名="MuName1"
Atemp.add(p)P =new 对象
p.对象ID=2
p.对象名="MyName2"
Atemp.add(p)P =new 对象
p.对象ID=3
p.对象名="MyName3"
Atemp.add(p)dataGrid.dataSource=atemp
dataGrid.DataBind
IBindingList 接口请参见
要求
命名空间: System.ComponentModel
有关此类型所有成员的列表,请参阅 IBindingList 成员。
实现 IBindingList 的类
类 说明
DataView 表示用于排序、筛选、搜索、编辑和导航的 DataTable 的可绑定数据的自定义视图。
DataViewManager 包含 DataSet 中每个 DataTable 的默认 DataViewSettingCollection。 备注
该接口由 DataView 类实现。方法的实现应该与该方法在 DataView 类中实现表现出相同的行为。当调用 ApplySort 或 RemoveSort 方法时,应该用 Reset 枚举引发 ListChanged 事件。当调用 AddNew 方法时,应该用带有适当索引的 ItemAdded 枚举引发 ListChanged 事件。添加的行处于这样一种状态,此时在 DataGrid 控件上按下 ESC 键时可以将新行移除。在此行上再次用 ItemAdded 枚举引发 ListChanged 事件指示该项现在是不处于“新”状态的行。当移除某项或调用新行(如果该行实现 IEditableObject)上的 CancelEdit 方法时,应该用带有适当索引的 ItemDeleted 枚举引发 ListChanged 事件。示例
[Visual Basic, C#, C++] 下面的示例提供 IBindingList 接口的简单实现。CustomerList 类将客户信息存储在一个列表中。此示例假定已使用了 Customer 类,该类可以在 IEditableObject 类中的示例中找到。
[C#]
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;
}[C++]
public __gc class CustomersList : public CollectionBase, public IBindingList {
private: ListChangedEventArgs* resetEvent;
ListChangedEventHandler* onListChanged; __event ListChangedEventHandler* ListChanged;
// Implements IBindingList.
__property bool IBindingList::get_AllowEdit()
{
return true;
} __property bool IBindingList::get_AllowNew()
{
return true;
} __property bool IBindingList::get_AllowRemove()
{
return true;
} __property bool IBindingList::get_SupportsChangeNotification()
{
return true;
} __property bool IBindingList::get_SupportsSearching()
{
return true;
} __property bool IBindingList::get_SupportsSorting()
{
return true;
} // Methods.
Object* IBindingList::AddNew()
{
Customer* c = new Customer(__box(this->Count)->ToString());
List->Add(c);
return c;
} // Unsupported properties.
__property bool IBindingList::get_IsSorted()
{
throw new NotSupportedException();
return false;
} __property ListSortDirection IBindingList::get_SortDirection()
{
throw new NotSupportedException();
return ListSortDirection::Ascending;
}
__property PropertyDescriptor* IBindingList::get_SortProperty()
{
throw new NotSupportedException();
return 0;
}
// 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();
return 0;
} void IBindingList::RemoveIndex(PropertyDescriptor* property)
{
throw new NotSupportedException();
} void IBindingList::RemoveSort()
{
throw new NotSupportedException();
} // Worker functions to populate the list with data.
static Customer* ReadCustomer1()
{
Customer* cust = new Customer(S"536-45-1245");
cust->FirstName = S"Jo";
cust->LastName = S"Brown";
return cust;
} static Customer* ReadCustomer2()
{
Customer* cust = new Customer(S"246-12-5645");
cust->FirstName = S"Robert";
cust->LastName = S"Brown";
return cust;
}
protected: virtual void OnListChanged(ListChangedEventArgs* ev)
{
if (onListChanged != 0)
{
onListChanged(this, ev);
}
} void OnClear()
{
List->Clear();
} void OnClearComplete()
{
OnListChanged(resetEvent);
} void OnInsertComplete(int index, Object* value)
{
Customer* c = __try_cast<Customer*>(value);
c->Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType::ItemAdded, index));
} void OnRemoveComplete(int index, Object* value)
{
Customer* c = __try_cast<Customer*>(value);
c->Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType::ItemDeleted, index));
} void OnSetComplete(int index, Object* oldValue, Object* newValue)
{
if (oldValue != newValue)
{ Customer* oldcust = __try_cast<Customer*>(oldValue);
Customer* newcust = __try_cast<Customer*>(newValue); oldcust->Parent = 0;
newcust->Parent = this;
OnListChanged(new ListChangedEventArgs(ListChangedType::ItemAdded, index));
}
}
public: // Constructor
CustomersList() {
resetEvent = new ListChangedEventArgs(ListChangedType::Reset, -1);
} void LoadCustomers()
{
IList* l = static_cast<IList*>(this);
l->Add(ReadCustomer1());
l->Add(ReadCustomer2());
OnListChanged(resetEvent);
} __property Customer* get_Item(int index)
{
return static_cast<Customer*>(List->Item[index]);
} __property void set_Item(int index, Customer* value)
{
List->Item[index] = value;
} int Add (Customer* value)
{
return List->Add(value);
} Customer* AddNew()
{
return __try_cast<Customer*>(static_cast<IBindingList*>(this)->AddNew());
} void Remove (Customer* value)
{
List->Remove(value);
}private public: // Called by Customer when it changes.
void CustomerChanged(Customer* cust)
{
int index = List->IndexOf(cust); OnListChanged(new ListChangedEventArgs(ListChangedType::ItemChanged, index));
}
IsReadOnly IsSynchronized IsFixedSize Capacity Count
False False False 16 1 就是不知如何显示所有对象的属性
to Eddie005(♂) 暴赱 『零零伍』 (︶︵︶)
你的例子可直接运行吗?