Delete record from DataView

I have DataViewone that is populated with a list of files from a database table. I want to iterate through a DataView to see if there are any files of a certain type, and if so, do something with this record and then delete it from the DataView.

I encoded it as follows, but something is missing there - I can iterate over the object and then delete the object from it, as this 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, dlFilesused DataListto display items in DataView. Deleted files are displayed differently, and therefore should not be referenced when repeating through DataList.

+3
source share
2 answers

We can do it

  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();
     }
  }            
+2

, , datatable, datatable .

0

Source: https://habr.com/ru/post/1786062/


All Articles