In this article I will explain How to open or
view files in gridview. While working with gridview we face a situation where
we want to show filename in gridview and want to open them on click. Like given
in Attached Image.
Open/View Files in Gridview(Click Image to Enlarge view)
Follow
the following process to open/view the Image or Doc Files from gridview.
1. Save
names of Images or docx to database and files to folder inside the application.
2. Now
bind the name of the files in grdiview from database.
3. Fire
a Gridview Event i.e. RowCommand to handle Linkview button click event(When we
click on FileName or Open File LinkView button to open that particular files).
4. Write
the code on GridView RowCommand to open files or view Files.
Create
a table:
Create s small Application to Demonstrate the
concept:
Add a simple Aspx Page and Place a Gridview inside that page. Now
customize it like given below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<asp:GridView ID="grdAttachment" DataKeyNames="Attachment_ID" runat="server" AutoGenerateColumns="False" OnRowCommand="grdAttachment_RowCommand" CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="UserName" ReadOnly="true" HeaderText="ArticleNo" >
<ItemStyle Width="30px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Document">
<ItemTemplate>
<asp:LinkButton ID="lnkFile" runat="server" CausesValidation="false" CommandArgument='<%# Bind("FilePath") %>' CommandName="ViewFile" Text='<%# Bind("FilePath") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Open File">
<ItemTemplate>
<asp:LinkButton ID="LnkEdit" CausesValidation="False" CommandArgument='<%# Bind("FilePath") %>' CommandName="ViewFile" runat="server" Text='OpenFile'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div></center>
</form>
</body>
</html>
Now write the following code in Code behind:
Add Following Namespace:
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
Now Bind Gridview on Page Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
FillGrid();
}
private void FillGrid()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from
Attachment",
con);
DataTable dt = new
DataTable();
adp.Fill(dt);
grdAttachment.DataSource = dt;
grdAttachment.DataBind();
con.Close();
}
Now write following code on Row Command to Open or View Files:
protected void grdAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewFile")
{
string
fileName = Server.MapPath("~/Attachment/" +
e.CommandArgument.ToString());
Process process = new
Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName =
fileName;
process.Start();
}
}
Note: In this
article, I have explained how to open or view files like Images & Docx
files in gridview in simple steps (using Asp.Net(C#)). If you have any
doubt you can ask by leaving your query in comment section or you can email me
on programmerskill@gmail.com
In previous article I have explained how to generate PDF in ASP.NET with different Control inside the PDF like RadioButtonlist & Textboxes. Please read it if find it useful.
In previous article I have explained how to generate PDF in ASP.NET with different Control inside the PDF like RadioButtonlist & Textboxes. Please read it if find it useful.