我有一个继承BindingList并实现IBindingListView的自定义可排序列表:
public class MySortableView<T> : BindingList<T>, IBindingListView
{
...
}我使用这个自定义列表作为我在网格上的数据源:
gridCustomers.DataSource = new MySortableView<Customer>(listCustomers);当我从网格中删除一行时,我的问题就出现了。在删除一行之前,我使用一个for循环将网格的内容复制到一个对象列表中,该循环遍历网格的行。然后将该行从对象列表中删除,然后再次将网格的DataSource设置为对象列表。
//Before delete:
listObjects = new List<object>();
for (int iIndex = 0; iIndex < gridCustomers.Rows.Count; iIndex++)
{
listObjects.Add(gridCustomers.Rows[iIndex].DataItem);
}
//After delete I set the datasource to listObjects
gridCustomers.DataSource = listObjects这会运行,但是在这样做之后,我不再能够对网格的内容进行排序,因为网格不再使用MySortableView。
因此,我希望能够做到以下几点:
Type listObjectsType;
//Get the type before removing the row
listObjectsType = gridCustomers.Rows[iIndex].DataItem.GetType();
//Set datasource back using the stored type.
gridCustomers.DataSource = new MySortableView<listObjectType>(listObjects);当我尝试以上操作时,我收到以下错误:
listObjectType是“字段”,但它的用法与“类型”类似
这看起来似乎是一种愚蠢的方式,但类型Customer存在于不同的程序集中。
请帮帮我!
我希望这一切都有意义。
我使用的是C#、.Net 4.0和entity框架。
谢谢
发布于 2012-03-06 20:34:17
您的DataGrid或其他文件中是否总是有Customer Type?也许你可以试试这样的方法:
Type listObjectsType = typeof(Customer);https://stackoverflow.com/questions/9568844
复制相似问题