In this article i am going to explain, how we can display any image in a grid view from the computer's local path. This article will be useful where you are dealing with some scenarios like, you have to display employee details or you have to bind the item list. In this case you can use the below code which will help you to display together with the other details of the employee/items, you can display their images and that too from your local system.
To start with design, first create a table in your database, suppose empdetails, which will have column as below-
Then create a gridview, which you can drag and drop from the ASP.NET toolbox controls and their code will be like below-
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="False">
<asp:BoundField HeaderText = "Employee Id" DataField="EmpId" />
<asp:BoundField HeaderText="Employee Name" DataField="Empname" />
<asp:TemplateField HeaderText="Employee Image">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("EmpImg")%>'/>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
Now we need to bind this grid view from the database, for this we will write the code to display the data from database in code behind page-
On the page_load method, i am loading the grid again, because all the changes occured in the database will get reflected each time page will refresh.
protected void Page_Load(object sender, EventArgs e)
And now write the method Bindgrid() as below-
Public void Bindgrid()
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT EmpId,Empname,empimg from empdetails", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
gvImages.DataSource = dt;
That's all. Now run the code and see the output. Your all the details will be shown.