質問

I want to get all rows of a ultragrid even deleted rows. I have used the following code to select only one column of this grid but it caused an error:

"Deleted row information cannot be accessed through the row."

((DataTable)grid1.DataSource).AsEnumerable()
                             .Select(row => row.Field<String>("filedName"))
                             .ToList();

I have used .AcceptChanges() to solve this error but now the result does not contain deleted rows.

Can anyone help me get all rows including deleted rows of this ultragrid?

役に立ちましたか?

解決

There is no way to reach directly a DataRow after you have applied the Delete method to it. If you call AcceptChanges then this DataRow will be removed from the collection like it never was there. (And thus, if you haven't yet updated the database, the row will never be removed from the database)

To reverse the effect of a Delete method the only possible way is through the use of RejectChanges method, but, after that, the row is no more deleted.

I can't give you an example in LinQ, but, generally you need to do something like this

for(int x = 0; x < datatable.Rows.Count; x++)
{
   DataRow r = dataTable.Rows[x];
   if(r.RowState == DataRowState.Deleted)
   {
       r.RejectChanges();
       .... //do you stuff
       r.Delete(); // redelete the row,
   }
}

Also, if you are interested only in the Deleted rows, you could change the code above in this way to extract, from the original table, the subset of deleted rows.

DataTable deletedRowsTable = datatable.GetChanges(DataRowState.Deleted);
for(int x = 0; x < deletedRowsTable.Rows.Count; x++)
{
    .....
    // no more if needed here, because the rows are all deleted
    .....
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top