0

What I need to do is to capture the data before being loaded in the dgv and change one of the properties. What I have now is a generic method which I'm using to create the DataSource for my dgv:

dgv.DataSource = new SortableBindingList<T>(result.ResultEntities);

And what I want to do is to get this data, manipulate it the way I need and then return the changed data for loading in the dgv.

I tried to accomplish this by simply doing this:

SortableBindingList<MyType> someVar = (SortableBindingList<MyType>)dgvMydgv.DataSource;

well, it seems it's not the way to do this.

4

1 回答 1

1

如果将诸如 sortableBindingList 之类的局部变量绑定到 datagridview,则在 datagrid 数据绑定成功后将无法访问它。在“DataBound()”事件触发后,datasource 将为 NULL。

供参考:

如果将 a 绑定SqlDataSource到 datagridview,则可以在绑定数据源后导出 sortablebindinglist。

您必须读出每一行中的每一列并将其保存在新定义的 sortablebindinglist 中,然后将数据源重新绑定到 gridview:

foreach (GridViewRow gvrMyRow in gvFields.Rows)
{
     foreach (TableCell tcMyCell in gvrMyRow.Cells)
     {
         string sMyValue = tcMyCell.Text;
     }
}

记住:

保持对原始填充列表的引用是正常的方式,您应该以这种方式实现。它更容易找到条目,操作条目等。

于 2013-02-20T14:43:28.927 回答