Hi there again,
No problem at all, unfortunatelly we have to do it manually (i little bit
more coding). BTW i completely forgot vb.net differes from c# in array
declaration and there was a bug in my last snippet. Please forgive me but i'm
strictly c# man :) Anyway, it should go as following:
-- begin aspx page code --
<asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="name" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Created
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="created" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Size
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="Size" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
-- end aspx page code --
-- begin vb.net code behind --
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
_
Handles Me.Load
If Not IsPostBack Then
Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))
If directory.Exists Then
Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
Dim both(files.Length + directories.Length - 1) As System.IO.FileSystemInfo
Array.Copy(files, 0, both, 0, files.Length)
Array.Copy(directories, 0, both, files.Length, directories.Length)
dgFD.DataSource = both
dgFD.DataBind()
End If
End If
End Sub
Protected Sub dgFD_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgFD.ItemDataBound
Dim item As DataGridItem = e.Item
If item.ItemType = ListItemType.Item Or _
item.ItemType = ListItemType.AlternatingItem Then
Dim fileInfo As System.IO.FileSystemInfo = _
CType(item.DataItem, System.IO.FileSystemInfo)
Dim literal As Literal
' name
literal = CType(item.FindControl("name"), Literal)
literal.Text = fileInfo.Name
' creation time
literal = CType(item.FindControl("created"), Literal)
literal.Text = fileInfo.CreationTime.ToString()
If TypeOf fileInfo Is System.IO.FileInfo Then
'size
literal = CType(item.FindControl("length"), Literal)
literal.Text = CType(fileInfo, _
System.IO.FileInfo).Length.ToString()
End If
End If
End Sub
-- end vb.net code behind --
Hope it helps
Milosz
"rn**@rediffmail.com" wrote:
On Feb 20, 8:23 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Hi there guys,
He gets exception because data source contains two types of objects. Even if
they inherit from the same base class, data binder checks the actual type (it
does not matter if you use base class in source array because polymorphic
call to GetType() will return real type of every element) which can be
FileInfo or DirectoryInfo. This only happens when you use AutoGenerateColumns
because, grid control enumerates all properties for every DataItem (meaning
for every single row). To resolve the problem, declare columns directly (note
there are some incompatibilities such Name):
-- begin aspx code --
<asp:DataGrid runat="server" ID="dgFD" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="FullName" HeaderText="Name"/>
<asp:BoundColumn DataField="CreationTime" HeaderText="Created" />
</Columns>
</asp:DataGrid>
-- end aspx code -
-- begin vb.net code -
Dim directory As New System.IO.DirectoryInfo(Server.MapPath("~/"))
If directory.Exists Then
Dim files() As System.IO.FileSystemInfo = directory.GetFiles()
Dim directories() As System.IO.FileSystemInfo = directory.GetDirectories()
Dim both(files.Length + directories.Length) As System.IO.FileSystemInfo
Array.Copy(files, 0, both, 0, files.Length)
Array.Copy(directories, 0, both, files.Length - 1, directories.Length)
dgFD.DataSource = both
dgFD.DataBind()
End If
-- end vb code --
Hope this helps
"Mark Fitzpatrick" wrote:
First, have you checked to make sure that you aren't getting a null value or
empty array when you call the GetFileSystemInfos method? I can't think of
anything else that may cause it since the FileSystemInfo class is the base
of the FileInfo.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
<r...@rediffmail.comwrote in message
>news:11**********************@h3g2000cwc.googlegr oups.com...
Consider the following code:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dInfo As DirectoryInfo
dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))
dgFD.DataSource = dInfo.GetFiles("*.*")
dgFD.DataBind()
End Sub
</script>
<form runat="server">
<asp:DataGrid ID="dgFD" runat="server"/>
</form>
The above code works fine & populates the DataGrid with all the files
residing in the directory named "Folder1". But it doesn't get the sub-
directories residing in "Folder1". In order to populate the DataGrid
with both the files & sub-directories residing in "Folder1", I
replaced the line
dgFD.DataSource = dInfo.GetFiles("*.*")
with
dgFD.DataSource = dInfo.GetFileSystemInfos("*.*")
But now when I run the above code, ASP.NET generates the following
error:
Object does not match target type.
pointing to the line
dgFD.DataBind()
What is wrong with the above code? How do I populate the DataGrid with
both the files as well as the sub-directories residing in the
directory named "Folder1"?- Hide quoted text -
- Show quoted text -
Thanks a lot, Milosz. Your code certainly did help a lot but as you
have pointed out, there are some incompatibilities like Name. Length
(used to find the size of a file) is another such incompatibility. How
do you overcome these incompatibilities?
Thanks once again....