Prady
Prady

Reputation: 11330

Best way to display data in a gridview in C#

I have data of the format

Filename   Status
abc.txt     Found
xyz.txt     Not Found

I need to display it on a gridview.

Upvotes: 0

Views: 1228

Answers (3)

Renatas M.
Renatas M.

Reputation: 11820

If you are not sure that your file names are unique you can use a dictionary as IndigoDelta mentioned, but check if the key already exist before adding new entry:

Dictionary<string, string> files = new Dictionary<string,string>();
string status = "Not Found";
if (File.Exists("abc.txt"))
   status = "Found";

//Check if key exists
if (files.ContainsKey("abc.txt"))
   files.Add("abc.txt", status);
else
   files["abc.txt"] = status;

Upvotes: 0

Samidjo
Samidjo

Reputation: 2355

Collections are best suited. Create a File class where you can put your fields like this:

class File
{
    private string _fileName;

    public string fileName{
       get { return _fileName;}
           set { _fileName= value;}
    }

        private string _status;

    public string status{
       get { return _status;}
       set { _status= value;}
    }
}

Then you add every instance to a list.

    /* Create Instances */
    GridView grid = new GridView();
    List<File> files = new List<File>();

    /* create and fill File instance */
    File f = new File();
    f.status = "WhatEverString";
    f.fileName = "WhatEverString";

    /* Add file instance to the list*/
    files.add(f);

    /* Bind data to GridView*/
    grid.DataSource = Files;
    grid.DataBind();

Now we only have to refer to every field to its place in ASP side, Stack Overflow question How to bind a List to a gridview? should help.

Upvotes: 4

IndigoDelta
IndigoDelta

Reputation: 1481

A Dictionary or Array/List of KeyValuePair would work.

Upvotes: 2

Related Questions