Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Record from a DataView

I have a DataView which has been populated with a list of files from a database table. I want to iterate through the DataView to see if there are any files there of a particular type, and if so, do something with that record, and then remove it from the DataView.

I've coded this as follows, but there's something missing - I can iterate over an object and then remove an object from it, since that will affect the iterator.

Any suggestions?

DataView dv = new DataView();
dv = ds.Tables[3].DefaultView;
dlFiles.DataSource = dv;
dlFiles.DataBind();
for (int j = 0; j < dv.ToTable().Rows.Count; j++) {
    if (dv.ToTable().Rows[j]["FilePath"].ToString().ToLower().Contains(".pdf")) {
        //do something with this record and remove it from the dataview
    }
}

As a note, dlFiles is a DataList used to display the the items in the DataView. The files removed are displayed differently, and so should not be referenced when iterating through the DataList.

like image 519
Elie Avatar asked Jul 14 '26 14:07

Elie


1 Answers

We can do like this,

  DataView dv = new DataView();
  dv = ds.Tables[3].DefaultView;      
  for (int j = 0; j < dv.ToTable().Rows.Count; j++)
  {
     if (dv.ToTable().Rows[j]["FilePath"].ToString().ToLower().Contains(".pdf"))
     {
         dv.Table.Rows.RemoveAt(j);
         dv.AcceptChanges();
     }
  }            
like image 89
thevan Avatar answered Jul 17 '26 16:07

thevan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!