zkanoca
zkanoca

Reputation: 9918

Conditional file listing on gridView in ASP.NET?

I am writing a file management web page for a friend of mine. I select the folder using a DropDownlist element. When the index changed it populates gridview.

For preventing user slips, I have decided not to delete the file when Delete button is clicked. I change the name of the deleted file adding a suffix.

For example if I delete a file.pdf via Deletebutton, it is renamed as file.pdf_zkanoca_deleted_1411472294

After populating the gridview content, renamed files are still listed. My listFiles() method is as the following:

public void listFiles(string selectedFolder)
{
    var dir = new DirectoryInfo(selectedFolder);

    gridView1.DataSource = dir.GetFiles();
    gridView1.DataBind();
}

What I want is to check whether filename contains "_zkanoca_deleted_" string before binding data source to gridview. If it contains that string it will not be listed.

I think a foreach loop will solve my problem. But I could not imagine how to structure it.

Upvotes: 0

Views: 124

Answers (1)

Steve
Steve

Reputation: 216263

Use the IEnumerable.Where extension

gridView1.DataSource = dir.GetFiles().Where(x => !x.Name.Contains("_zkanoca_deleted_")).ToList();

As explained in the docs the Where extension filters a sequence using a predicate.
In this case you could use the FileInfo property Name to check if it contains the forbidden substring and exclude that FileInfo from the sequence binded to the gridview.

Upvotes: 3

Related Questions