我正在尝试对列出目录文件的gridview进行排序。
我试图以各种不同的方式对资源进行排序:
http://forums.asp.net/t/963636.aspx?Default+sort+order+in+GridView sorting and paging with gridview asp.net http://forums.asp.net/t/1076872.aspx?Gridview+how+to+find+a+column+by+it+s+column+name+rather+than+it+s+Cell+index+ http://forums.asp.net/t/963636.aspx?Default+sort+order+in+GridView
但是,每次重新加载页面时,文件都不按升序排序。
我希望能够通过文件"价值" [列:文件名],因为那样可以得到我想要的结果,但没有任何效果。
我也尝试了VB.NET Sort files in directory by alphanumeric:
For Each filePath As String In filePaths.OrderBy(files)
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
'files.Add(New ListItem(Path.GetFileName(filePath)
Next
但是从行filePaths.OrderBy(files)
获取错误,因为括号内的信息是错误的。
有什么建议吗?
仅供参考:出于安全目的,Directory.GetFiles
的路径是假的
Dim filePaths() As String = Directory.GetFiles("\\...\...\uploads\attachments\")
Dim files As List(Of ListItem) = New List(Of ListItem)
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
DisplaySup.DataSource = files
DisplaySup.DataBind()
DisplaySup.Sort("File Name", System.Web.UI.WebControls.SortDirection.Ascending)
'If [String].IsNullOrEmpty(DisplaySup.SortExpression) Then
'DisplaySup.Sort("SortExpression", SortDirection.Ascending)
'DisplaySup.DataBind()
'End If
HTML
<asp:GridView ID="DisplaySup" runat="server" align="center" AutoGenerateColumns="False" BorderColor="Black" BorderStyle="Solid" Font-Size="Small">
<Columns>
<asp:TemplateField HeaderText = "Delete File" SortExpression="Value">
<ItemTemplate>
<asp:button ID = "btnDelete" cssClass = "submit-btn small" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "File Name" SortExpression="Value">
<ItemTemplate >
<asp:LinkButton ID="lnkDownload" Text = '<%# Eval("Value").ToString().Substring(39) %>' CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#BDBDAE" Font-Underline="False"
CssClass="linkNoUnderline" HorizontalAlign="Left" />
</asp:GridView>
更新:我还尝试了以下示例GridView Sorting Question,并再次对gridview进行排序
答案 0 :(得分:0)
找到解决问题的方法。
我引用了:Sorting Directory.GetFiles()
并执行以下操作以对目录进行预排序,然后在gridview中列出文件:
Dim filePaths() As String = Directory.GetFiles("\\...\uploads\attachments\")
'Sort the directory
Dim FilesStr() As String
FilesStr = filePaths
Array.Sort(FilesStr)
'Get each file
Dim files As List(Of ListItem) = New List(Of ListItem)
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
DisplaySup.DataSource = files
DisplaySup.DataBind()