Elie
Elie

Reputation: 13855

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.

Upvotes: 3

Views: 2736

Answers (2)

thevan
thevan

Reputation: 10364

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

Upvotes: 2

zhangrt
zhangrt

Reputation: 1

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

Upvotes: 0

Related Questions