getfiles, minus the path?

G

Guest

I'mtrying to display a list of file names in a directory but using getfiles
displays the entire path. Is there any way to just display the file name?
I'm putting the file names into a datagrid:

'create file list for defect images datagrid
Dim imagepath As String
imagepath = Server.MapPath("\xxxxx\xxxxxx\examples\")
dgfiles.DataSource = Directory.GetFiles(imagepath)
dgfiles.DataBind()

I I'm thinking is with the datagrid I can change the displayed column value
and make it a link to display an image based on the file click on in the
datagrid.

Also I wish the dropdownlist allowed a separate value parameter to set, i.e.
display = "file.gif", value for item = "c:/xxx/xxxx/file.gif".

thanx.
 
K

Ken Cox [Microsoft MVP]

Hi Chris,

I think you just need two more bits to have this figured out. First, you can
make changes in the content of the datagrid row when the row is bound to its
data. Therefore, using the ItemDataBound event.

Next, you want to strip off all the path stuff. That's done easily with
System.IO.Path.GetFileName.

When you put them together, you get something like the code below.

Let us know if this helps?

Ken
Microsoft MVP ASP.NET
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'create file list for defect images datagrid
Dim imagepath As String
imagepath = Server.MapPath("bin")
dgfiles.DataSource = _
System.IO.Directory.GetFiles(imagepath)
dgfiles.DataBind()

End Sub

Private Sub dgfiles_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgfiles.ItemDataBound
Dim strFilename As String
strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
e.Item.Cells(0).Text = strFilename
End Sub

<asp:datagrid id="dgfiles" runat="server"></asp:datagrid>
 
G

Guest

Thanx, this works great. It sure beats the routine I wrote to strip off the
file name, etc.
Also, do you know how to access the column info. The datagrid's header is
"Item". Normally I would use <asp:BoundColumn... but that's with a datasource
coming from a database. How do you handle it from the file object?
I'd like to change the header and make the column a link to the actual file.
I've done it via a database datasource.

Thanx.
 
K

Ken Cox [Microsoft MVP]

Hi Chris,

To change the header, check for the Header type and then insert your new
text. Here's the updated code:

Private Sub dgfiles_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgfiles.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
e.Item.Cells(0).Text = "New Header Here"
Exit Sub
End If
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
Dim strFilename As String
strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
e.Item.Cells(0).Text = strFilename
End If
End Sub

Ken
MVP [ASP.NET]
Toronto
 
G

Guest

thanx for the code start! It really helped me understand.

Ken Cox said:
Hi Chris,

To change the header, check for the Header type and then insert your new
text. Here's the updated code:

Private Sub dgfiles_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgfiles.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
e.Item.Cells(0).Text = "New Header Here"
Exit Sub
End If
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
Dim strFilename As String
strFilename = System.IO.Path.GetFileName(e.Item.Cells(0).Text)
e.Item.Cells(0).Text = strFilename
End If
End Sub

Ken
MVP [ASP.NET]
Toronto


Chris said:
Thanx, this works great. It sure beats the routine I wrote to strip off
the
file name, etc.
Also, do you know how to access the column info. The datagrid's header is
"Item". Normally I would use <asp:BoundColumn... but that's with a
datasource
coming from a database. How do you handle it from the file object?
I'd like to change the header and make the column a link to the actual
file.
I've done it via a database datasource.

Thanx.
 
K

Ken Cox [Microsoft MVP]

Hey Chris,

Drop in any time. We've got a great group here. We all need a boost at some
point.

Ken
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top