C# .NET - DataSet, Deleted row information cannot be accessed through the row.

Asked By Dale H on 26-Jan-11 02:52 PM
Please ignore:  Found another way...
I have the following code which deletes the selected rows from the datatable.  First time through it works fine, but after making another selection in the dropdown on the gridview i get the following:

Deleted row information cannot be accessed through the row.

I reload the table from the ViewState each time, but it appears the rowstate status of deleted is persisting.  Is there a way to avoid that??
 

DataTable dt = (DataTable)this.ViewState["Table"];

dt.PrimaryKey = new DataColumn[] { dt.Columns["Name"] };


int
rIndex = 0; 
foreach (DataRow dr in dt.Rows) 
{ 
  DataRow drow = dt.Rows[rIndex]; 
  if(drow.ItemArray[3].ToString() != prefSelected) 
  { 
  dt.Rows[rIndex].Delete(); 
  } 
  rIndex++;
  
}
  
GridView1.DataSource = dt;
  
GridView1.DataBind();

Vic S replied to Dale H on 03-Mar-11 03:24 PM
Try using this:

int rIndex = 0; 
foreach (DataRow dr in dt.Rows) 
{ 
  DataRow drow = dt.Rows[rIndex]; 
  if drow.RowState != DataRowState.Deleted && (drow.ItemArray[3].ToString() != prefSelected) 
  { 
  dt.Rows[rIndex].Delete(); 
  } 
  rIndex++;
  
}