I met a problem that after the DataTable using Select and Sort method.
The DataRow it fetch cannot get the RowIndex from the source/origin DataTable by using IndexOf.
Detail information:
I have 2 DataTable.
First one is Source DataTable SourceDataTable
Second one is a Copy CopyDataTable.
I use CopyDataTable for selection and Sort. Just like below: CopyDataTable.Select(Statement) CopyDataTable.DefaultView.Sort = Sort Statement CopyDataTable = CopyDataTable.DefaultView.ToTable
3.Then I loop the CopyDataTable with For Each Row
and want to know the DataRow fetch from the CopyDataTable during the iteration about the index from the SourceDataTable by the code below.
For each DataRow in CopyDataTable SourceDataTable.Rows.IndexOf(DataRow)
But it pops up the message that it cannot find the index.
@opas1216 That is most probably because, the datarow does not belong to that Datatable. Even if the row values are the same, I think the reference to the datatable matters in this case.
@opas1216
there could be few options that can be checked:
in one scenario we marked the SourceDataTable with an additional rowIndex Column, did all the actions on a copyDataTable and when forwarding the date filtered data we just removed the rowIndex Column.
If you are interested on how to add the rowIndex let me know.
Another approach could be: lets assume a row in CopyDataTable was identified (called drCFound)
SequenceEquals via the itemArrays can be checked for the index
SourceDataTable.asEnumerable.toList.FindIndex(function ( r ) r.ItemArray.SequenceEqual(drCFound.ItemArray))