Cannot not get the row index by IndexOf after the DataTable was Select and Sort

Hi,

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:

  1. I have 2 DataTable.
    First one is Source DataTable SourceDataTable
    Second one is a Copy CopyDataTable.

  2. 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.

Please provide any ideas/suggestion

Thanks so much

@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.

Do you have any idea how to solve this problem?

Since after Select and Sort method, the result has to assign to another variable.
So I couldn’t just use the SourceDataTable repeatedly.

@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))

More Info here:


Let us know if you need further help

1 Like

find starter help here:
FindOriginIndex_ByCopyRow.xaml (7.4 KB)