开发者

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.

  • How do I hold these values in?
  • Should I use a multidimensional array or other collections?
  • Which one would be best s开发者_如何学Gouited?


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.


A Dictionary or Array/List of KeyValuePair would work.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜