[英]Best way to display data in a gridview in C#
我有格式的數據
Filename Status
abc.txt Found
xyz.txt Not Found
我需要在 gridview 上顯示它。
Collections 最適合。 創建一個文件 class ,您可以在其中放置您的字段,如下所示:
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;}
}
}
然后將每個實例添加到列表中。
/* 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();
現在我們只需要在 ASP 端引用每個字段到它的位置,堆棧溢出問題如何將列表綁定到 gridview? 應該有幫助。
KeyValuePair的字典或數組/列表將起作用。
如果您不確定您的文件名是唯一的,您可以使用 IndigoDelta 提到的字典,但在添加新條目之前檢查密鑰是否已經存在:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.