使用自定义数据源从网格获取类型,该数据源继承BindingList <t>并使用C#和.NET 4.0实现IBindingListView </t>

时间:2012-03-05 15:04:08

标签: c# types bindinglist

我有一个自定义的可排序列表,它继承了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是一个&#39;字段&#39;但是像'&#39;

一样使用

这似乎是一种愚蠢的方式,但Customer类型存在于不同的程序集中。

请帮助!!

我希望这一切都有道理。

我使用的是C#,。Net 4.0和实体框架。

由于

1 个答案:

答案 0 :(得分:0)

您的DataGrid或其他人是否也会有客户类型? 也许你可以尝试这样的事情:

Type listObjectsType = typeof(Customer);