KrisW
KrisW

Reputation: 165

DataTable check if row is last in collection

Say I have a DataTable like so:

state of D is DataViewRowState.Deleted and lets say O are rows that have not been touched

index: 0 | state: O  
index: 1 | state: O  
index: 2 | state: O  
index: 3 | state: D
index: 4 | state: D

Say I delete the row at index 2 (I set the row state to deleted). Is there a way to know if row at index 1 is the last non-deleted row in the collection without "walking forward" and checking the rows state of each record after index 1?

Upvotes: 0

Views: 794

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460238

You can use LINQ:

int deletedIndex = 2;
DataRow firstNonDeletedBeforeDeletedIndex = table.AsEnumerable()
    .Where((row, index) => index < deletedIndex && row.RowState != DataRowState.Deleted)
    .LastOrDefault(); // null if none found

Upvotes: 2

Related Questions