Reformat Number on Databinding

S

Sam Collett

I have the following that lists *.doc files in a folder. How do I list
other types as well (I have tried using
dirInfo.GetFiles("*.doc;*.rtf;*.pdf") but it does not work)?

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<script language="VB" runat="server">
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub BindData(ByVal compareMethod As CompareByOptions)
Dim dirInfo As New
DirectoryInfo(Server.MapPath("/path/to/folder/"))
Dim fileInfoArray() As FileInfo = dirInfo.GetFiles("*.doc")
Array.Sort(fileInfoArray, New
CompareFileInfoEntries(compareMethod))
fileList.DataSource = fileInfoArray
fileList.DataBind()
End Sub


Public Enum CompareByOptions
FileName
LastWriteTime
Length
End Enum

Public Class CompareFileInfoEntries
Implements IComparer

Private compareBy As CompareByOptions =
CompareByOptions.LastWriteTime

Public Sub New(ByVal cBy As CompareByOptions)
compareBy = cBy
End Sub

Public Overridable Overloads Function Compare(ByVal file1 As Object,
_
ByVal file2 As Object) As Integer Implements
IComparer.Compare
'Convert file1 and file2 to FileInfo entries
Dim f1 As FileInfo = CType(file1, FileInfo)
Dim f2 As FileInfo = CType(file2, FileInfo)

'Compare the file names
Select Case compareBy
Case CompareByOptions.FileName
Return String.Compare(f1.Name, f2.Name)
Case CompareByOptions.LastWriteTime
Return DateTime.Compare(f2.LastWriteTime, f1.LastWriteTime)
Case CompareByOptions.Length
Return f1.Length - f2.Length
End Select
End Function
End Class

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindData(CompareByOptions.LastWriteTime)
End If
End Sub

</script>
<html>
<head><title>List Files</title>
<link href="../style/default.css" rel="stylesheet" type="text/css">
</head>
<body>
<asp:DataGrid runat="server" id="fileList" AutoGenerateColumns="False"
AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-Font-Bold="True">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataTextField="Name" HeaderText="File Name" Target ="_blank" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write
Time"
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="File Size"
DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:DataGrid>
</body>
</html>

Also the file size is in bytes. How do I change it to kilobytes (or
megabytes if over a certain size)?

i.e.

File Name, Last Write Time, File Size
Doc1.doc, 01/09/2003, 70,144 bytes
Doc2.doc, 03/06/2003, 39,936 bytes
Doc3.doc, 17/04/2003, 37,376 bytes

becomes:

File Name, Last Write Time, File Size
Doc1.doc, 01/09/2003, 68.5 kb
Doc2.doc, 03/06/2003, 39 kb
Doc3.doc, 17/04/2003, 36.5 kb

Can the code also be cleaned up (what can be removed?)
 
S

Sam Collett

Is it not possible to manipulate data output from a datagrid then?
What is the alternative to a Datagrid?
Back to ASP then.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top