Displaying a Directory's Files in a DataGrid
To display a directory's files in a DataGrid (or DataList or Repeater) all we need to do is assign the String array or FileInfo array to the DataGrid's DataSource property and then call the DataGrid's DataBind() method. For this example, we'll use the DirectoryInfo.GetFiles() method instead of the Directory.GetFiles() method. If we opted to use the Directory.GetFiles() method then all we'd be able to show in the DataGrid is the file's name. By using the DirectoryInfo.GetFiles() method instead, we can display other attributes of the file, such as its size, last modified date, and so on.
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim dirInfo as New DirectoryInfo(Server.MapPath("" ))
articleList.DataSource = dirInfo.GetFiles("*.aspx" )
articleList.DataBind()
End Sub
</script>
<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="File Size"
ItemStyle-HorizontalAlign="Right"
DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:DataGrid>
Message édité par Vectteur le 15-08-2006 à 15:00:51
---------------
ceci est un bloc de texte