3

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.

Elie
  • 13,693
  • 23
  • 74
  • 128

2 Answers2

2

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();
     }
  }            
thevan
  • 10,052
  • 53
  • 137
  • 202
0

Well,use a new object such as datatable,copy the new data to this datatable,and bind your control.

zhangrt
  • 11
  • 2