G
Guest
I inherited some code that binds a dataset/datatable to a datagrid. When the
user highlights some rows the code loops thru a DataView of the grid to act
on those rows. This works fine, unless you first click a column to sort by
that column. Then the datagrid rows are not in the same sequence as the
datasource table. I'm thinking this is a definite bug in this code, but I'm
wondering what the point of doing things this way might have been. I mean
why bother with CurrencyManager and DataView if you can just access the
elements via datagrid1[row, col]?
Here's an example of the loop which seems overkill to me (besides not
working if you sort first!):
CurrencyManager mgr = (CurrencyManager)
dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
DataView dv = (DataView)mgr.List;
for (int i = 0; i < dv.Count; ++i)
{
if (dataGrid1.IsSelected(i))
{
DataRow row = dv.Table.Rows;
<get a field from row here...>
}
}
I mean, if the datagrid got sorted, the underlying datatable or dataview
isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
row 1 in the presorted datatable or dataview. Am I missing something in at
least the intention here?
user highlights some rows the code loops thru a DataView of the grid to act
on those rows. This works fine, unless you first click a column to sort by
that column. Then the datagrid rows are not in the same sequence as the
datasource table. I'm thinking this is a definite bug in this code, but I'm
wondering what the point of doing things this way might have been. I mean
why bother with CurrencyManager and DataView if you can just access the
elements via datagrid1[row, col]?
Here's an example of the loop which seems overkill to me (besides not
working if you sort first!):
CurrencyManager mgr = (CurrencyManager)
dataGrid1.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
DataView dv = (DataView)mgr.List;
for (int i = 0; i < dv.Count; ++i)
{
if (dataGrid1.IsSelected(i))
{
DataRow row = dv.Table.Rows;
<get a field from row here...>
}
}
I mean, if the datagrid got sorted, the underlying datatable or dataview
isn't also sorted is it? So highlighted row 1 in a sorted datagrid is NOT
row 1 in the presorted datatable or dataview. Am I missing something in at
least the intention here?