Open or View all type of files (Like Images, Docx, Pdf etc) in ASP.NET(C#)

Aman Sharma
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="">
<head runat="server">
    <form id="form1" runat="server">
    <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" />

   <asp:BoundField DataField="UserName" ReadOnly="true"  HeaderText="ArticleNo" >
   <ItemStyle Width="30px" />
   <asp:TemplateField HeaderText="Document">
   <asp:LinkButton ID="lnkFile" runat="server" CausesValidation="false" CommandArgument='<%# Bind("FilePath"%>' CommandName="ViewFile" Text='<%# Bind("FilePath"%>'></asp:LinkButton>
  <asp:TemplateField HeaderText="Open File">
   <asp:LinkButton ID="LnkEdit" CausesValidation="False" CommandArgument='<%# Bind("FilePath"%>' CommandName="ViewFile" runat="server" Text='OpenFile'></asp:LinkButton>
  <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />                         
   <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
   <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

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)

    private void FillGrid()
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
        if (con.State == ConnectionState.Open)
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Attachment", con);
            DataTable dt = new DataTable();
            grdAttachment.DataSource = dt;

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;


NoteIn 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

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.

Post a Comment

  1. This comment has been removed by the author.

  2. i have apply code but i am getting error in aspx.cs page

    CommandArgument and CommandName is not declared

    1. can you please share you code, So I can go through it.

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !