DataGrid Question

R

rn5a

In my application, I want to populate all the directories & files
existing in a directory on the server in a DataGrid. To ensure that
all the directories get listed first followed by all the files, I am
appending all the directories with 0 & all the files with 1.

Moreover, each directory & file listed in the DataGrid will have a
corresponding CheckBox. To differentiate between directories & files,
I am adding the string "*D" (without the quotes) to all the
directories & the string "*F" (again, without the quotes) to all the
files. I want to assign the corresponding CheckBoxes these IDs. For
e.g. if one of the directories is named "JOE", then the ID of the
CheckBox should be "chkJOE*D". Similarly, if there is a file named
"MyFile.txt", then its corresponding CheckBox should have the ID
"chkMyFile.txt*F".

There are 2 problems I am facing. First of all, I cannot display all
the directories & files in the DataGrid. Secondly, how do I ensure
that the ID of the CheckBoxes are assigned the string which I
mentioned above? This is the code:

<script runat="server">
Dim dInfo As DirectoryInfo

Sub Page_Load(.......)
dInfo = New DirectoryInfo(Server.MapPath("Directory1"))
Call ListFilesDirs(dInfo)
End Sub

Sub ListFilesDirs(ByVal dirInfo As DirectoryInfo)
Dim i As Integer
Dim iFind As Integer
Dim strURL As String
Dim strSize As String
Dim fInfo As FileInfo
Dim aList As ArrayList
Dim strListItem As String
Dim fsi As FileSystemInfo

aList = New ArrayList

For Each fsi In dirInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16)
Then
aList.Add("0" & fsi.Name & "<DIR>")
Else
fInfo = CType(fsi, FileInfo)
strSize = CStr(fInfo.Length)

If (strSize = 0) Then
strSize = "0.00"
End If

aList.Add("1" & fsi.Name & " - " & strSize & " MB - "
& fsi.CreationTime)
End If
Next
aList.Sort()

For i = 0 To aList.Count - 1
If (InStr(aList(i), "<DIR>") > 0) Then
strListItem = Left(aList(i), Len(aList(i)) - 6)
strListItem = Mid(strListItem, 2,
Len(strListItem)) & "*D"
Else
strListItem = Left(aList(i), InStr(aList(i), " -
") - 1)
strListItem = Mid(strListItem, 2,
Len(strListItem)) & "*F"
End If

'Response.Write(Mid(aList(i), 2, Len(aList(i))) &
"<br>")
dgDomain.DataSource = Mid(aList(i), 2, Len(aList(i)))
dgDomain.DataBind()
Next
End Sub
</script>

Note the commented Response.Write line just above the
dgDomain.DataSource line. When I uncomment this Response.Write line,
then all the directories & files get listed one after the other
correctly but they don't get populated in the DataGrid.

Note that the CheckBoxes should be displayed in the very first column
in the DataGrid. The Header of this first column should be a CheckBox
too so that checking/unchecking this Header CheckBox will check/
uncheck all the other CheckBoxes respectively.

Can someone please help me out with this?
 
G

Guest

dgDomain.DataSource = Mid(aList(i), 2, Len(aList(i)))
Note the commented Response.Write line just above the
dgDomain.DataSource line. When I uncomment this Response.Write line,
then all the directories & files get listed one after the other
correctly but they don't get populated in the DataGrid.

Sure, DataSource expected to be an array, and not a string.
 
R

rn5a

Sure, DataSource expected to be an array, and not a string.

Sorry, Alexey, but I couldn't exactly get your point. Could you please
elaborate? A code sample will be really appreciated.

Thanks...
 
G

Guest

Sorry, Alexey, but I couldn't exactly get your point. Could you please
elaborate? A code sample will be really appreciated.

Thanks...

The following line

dgDomain.DataSource = Mid(aList(i), 2, Len(aList(i)))

set a string as the DataSource of your grid.

I think that using

dgDomain.DataSource = aList

will work as you expected.
 
R

rn5a

The following line

dgDomain.DataSource = Mid(aList(i), 2, Len(aList(i)))

set a string as the DataSource of your grid.

I think that using

dgDomain.DataSource = aList

will work as you expected.- Hide quoted text -

- Show quoted text -

Thanks, Alexey; that has resolved the issued but I couldn't exactly
follow how does

dgDomain.DataSource = aList

resolve the issue & why doesn't

dgDomain.DataSource = aList(i)

populate the DataGrid? Could you please explain me this?

Moreover 2 new problems have also crept up. As you can see from the
code snippet I posted in the very first post in this thread, I am
appending all the directories with 0 & all the files with 1 so as to
ensure that all the directories get listed first followed by the files
but I would like to hide the 0's & 1's from the user. So if I just do

dgDomain.DataSource = aList

then the DataGrid displays all the directories preceded by 0 & all the
files preceded by 1. I can't use any String functions on aList since
it is an array & not a string. How do I get rid of the 0's & 1's from
the directories & files respectively when the DataGrid displays them?

The second problem is, as shown in the code snippet in post #1, if an
item happens to be a file, then I want to display the size along with
the creation time of the file in the DataGrid but I want to display
these 2 pieces of info in 2 different columns in the DataGrid. In
other words, the DataGrid should have 4 columns - the first column
must display a CheckBox for each item (which I have already taken care
of), the second column should display the directory/file name, the
third column should display the size of each item if that item happens
to be a file & the fourth column should display the date & time at
which the files/directories were created.

The Header of the first column in the DataGrid should be a CheckBox
(which I have already taken care of), the Header of the second column
should be 'NAME', the Header of the third column should be 'SIZE' &
the Header of the fourth column should be 'CREATION TIME'.

Now how do I make the DataGrid display the details in this manner?

Thanks once again for your helpful suggestions,

Regards,

Ron
 
G

Guest

On Feb 20, 5:24 am, (e-mail address removed) wrote:

First of all, I found mysterious on the screen

For i = 0 To aList.Count - 1
.....
dgDomain.DataSource = Mid(aList(i), 2, Len(aList(i)))
dgDomain.DataBind()
Next

By using this code you changed your data source (aList.Count - 1)
times.

This action

dgDomain.DataSource = ...
dgDomain.DataBind()

has to be without any loop!

dgDomain.DataSource = aList

resolve the issue & why doesn't

dgDomain.DataSource = aList(i)

populate the DataGrid? Could you please explain me this?

aList(i) does populate the grid. But you have to understand the
difference between an array/collection and an element of array.

In your case:

aList is a collection of many items - the set of directories and files
aList(i) is an item of this array, just one item - a file or directory

Assuming the first problem "First of all, I cannot display all the
directories & files in the DataGrid." is solved

then the DataGrid displays all the directories preceded by 0 & all the
files preceded by 1. I can't use any String functions on aList since
it is an array & not a string. How do I get rid of the 0's & 1's from
the directories & files respectively when the DataGrid displays them?

I think you always have at first the directories and then then files.

Isn't it?

The second problem is, as shown in the code snippet in post #1, if an
item happens to be a file, then I want to display the size along with
the creation time of the file in the DataGrid but I want to display
these 2 pieces of info in 2 different columns in the DataGrid. In
other words, the DataGrid should have 4 columns - the first column

If DataGrid should have 4 columns then the DataSource (in our case -
ArrayList) should have 4 "columns". The existing ArrayList cannot be
used as a multidimensional array directly and I don't know what *good*
solution I can suggest you here. If I were you perhaps I would go for
a another solution with custom class and so on, but because you have
already in-line code which is working (I assume), why do we making it
complicated with the DataGrid? Do not bind anything to grid, remove
that control from the page and generate your table in the loop

Response.Write "<table>"

For i = 0 To aList.Count - 1
.....
Next

Response.Write "</table>"

I think it will be better
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top