473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reformat Number on Databinding

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?)
Jul 21 '05 #1
1 3788
Is it not possible to manipulate data output from a datagrid then?
What is the alternative to a Datagrid?
Back to ASP then.

sa*********@lycos.co.uk (Sam Collett) wrote in message news:<20*************************@posting.google.c om>...
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?)

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: just1coder | last post by:
Does anyone know of anything that can be used with JavaScript to reformat HTML? Something like HTML Tidy, but that will clean the code on form submission or something to that effect.
0
by: Max | last post by:
I'm using user controls and keep getting errors like: active schema does not support the element Yet, the page runs just fine. When I go in and make changes to the HTML I get a pop-up like:...
1
by: Norton | last post by:
Hi all , i would like to make a small utility which allow me to reformat the sql statement from clipboard. I am finding ways which allow me to make use of MS SQL's Query Designer. ...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
8
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello to all, I want to know if DataBinding in asp.net 2,0 is better than to fill up the values of the controls of the following form: this.miControlTextBox.Text = valorParaControlTextbox; ...
1
by: sparks | last post by:
We are redoing an existing database and need to reformat all the number fields from integer to single fixed 3. their main thing is negative numbers and the decimal places. Can this be done in...
3
by: ccarter45 | last post by:
I really need help with this. Your tips are greatly appreciated: import java.util.Scanner; public class PhoneNumbers { public static void main(String args){ Scanner input; ...
18
by: Nick Keighley | last post by:
On 4 Jul, 15:26, Hallvard B Furuseth <h.b.furus...@usit.uio.nowrote: what's wrong with that?! wouldn't replacing spaces with tabs do that? Were disks ever *that* small? I've worked on...
3
by: =?Utf-8?B?a25vYmJ5?= | last post by:
i am trying to reformat my hard drive and reinstall win xp pro but for some reason i cant. here is the problem. when i boot from cd everything seems to go right untill it says press enter to run...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.