[英]Order files/list before binding them to gridview asp.net C#
Hi I have something like the code below, I want to sort/order the files in the folder lets say by name of the file in asc or dsc order, before binding to the gridview:嗨,我有类似下面的代码,我想在绑定到 gridview 之前,按 asc 或 dsc 顺序对文件夹中的文件进行排序/排序:
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
You can simply order by after foreach loop您可以简单地在 foreach 循环之后进行排序
files = files.OrderBy(x => x.Text).ToList();
For Order by descending按降序排列
files = files.OrderByDescending(x => x.Text).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.