Gridview - Open file

One of the columns of have on my Gridview is called " Filename". This columns display filenames.  How can I turn them into links where when a user click it and it opens a file? I store these file on my local drive. Thank you in advance.
Who is Participating?
RonaldZaalConnect With a Mentor Commented:
Here is some code, in the datagrid is a template colum with layout et which I didn't want to delete
I used this recently so it's a bit copied and pasted.
- The Datagrid to show files with creation date and clickable files as on your aspx page
- The filling of the datagrid with half coded how to sort
- The action in the datagrid when a user clicks on a file.

If you need more sorting code let me know.

				<asp:datagrid id="dgr_public" style="Z-INDEX: 109; LEFT: 1px; POSITION: absolute; TOP: 3px"
				runat="server" Width="643px" BorderColor="Gray" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" AutoGenerateColumns="False" CssClass="dg" >
				<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                    <EditItemStyle CssClass="dge" Font-Names="Arial" Font-Size="Smaller" />
                    <SelectedItemStyle BackColor="#669999" CssClass="dgs" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" Mode="NumericPages" />
                    <AlternatingItemStyle CssClass="dga" />
                    <ItemStyle CssClass="dgi" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" CssClass="dgh" Font-Bold="True" Font-Names="arial"
                        Font-Size="Smaller" ForeColor="White" />
                        <asp:TemplateColumn HeaderText="File Name">
                                   <asp:LinkButton runat="server" Text='<%# %>' CommandName="File" CausesValidation="false" ID="File" > </asp:LinkButton>
                        <asp:BoundColumn DataField="LastWriteTime" HeaderText="Created on" DataFormatString="{0:d}" >
                            <ItemStyle HorizontalAlign="Center" />
' Here is the sub to populate datagrid
' You have to import Imports System.IO in your page !
Public Sub GetFiles
' Show some mouse action when user hovers over it
            dgr_public.Attributes.Add("onmouseover", "window.status='Click Here'; return true;")
            dgr_public.Attributes.Add("onmouseout", "window.status=''; return true;")
' I sort my files
' Dim compareMethod As CompareByOptions = CompareByOptions.LastWriteTime
' if needed search or ask in comments
' I take my path from the web.config
 Dim part_reports_path As String = System.Configuration.ConfigurationManager.AppSettings("part_reports_path")
 For testing you can use : example : reports path = c:\data\projects\grid\files
Dim dirInfoPub As New DirectoryInfo(part_reports_path)
            If dirInfoPub.Exists Then
                Dim fileInfoArray() As FileInfo = dirInfoPub.GetFiles("*" & "*.xls")
' This way above I get only excel files 
' If you want the files sorted you need to apply a method for this
' Nice but not needed
' I you don't want it remove this next line, keep source and bind
                Array.Sort(fileInfoArray, New CompareFileInfoEntries(compareMethod))
                dgr_public.DataSource = fileInfoArray
                'error in filling your Datagrid  
            End If
' Now the action when the user clicks on a file : 
Public Sub Public_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgr_public.ItemCommand
            Dim filePath As String = System.Configuration.ConfigurationManager.AppSettings("part_reports_path")& "\"
            Dim filename As String = UCase(CType(e.Item.FindControl("File"), LinkButton).Text)
            If e.CommandName = "File" Then
                    Dim s As Stream = File.OpenRead(filePath & filename)
                    Dim buffer(s.Length) As Byte
                        s.Read(buffer, 0, CType(s.Length, Int32))
                    End Try
                    Response.ContentType = "application/octet-stream"
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & Replace(CType(e.Item.FindControl("File"), LinkButton).Text, " ", "_"))
                    '   Response.End()
                Catch ex As Exception
                    ' you have another error
                End Try
            End If
          End Sub

Open in new window

Pratima PharandeCommented:
asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#  DataBinder.Eval(Container.DataItem,"FILENAME") %>' Text="Go to event"></asp:HyperLink>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.