Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to bind an image to girdview,gridview contain image control in it
Posted

Try to use the RowDataBound event and bind the imageurl to the image control in it.
 
Share this answer
 
Hi vijji,
Gridview with Image[^]
 
Share this answer
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class imageupload : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Demoserver;Initial Catalog=greeshma;User ID=hpfs;Password=hpfs");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
if (FileUpload1.PostedFile != null &&
FileUpload1.PostedFile.FileName != "")
{
Byte[] imageSize = new Byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadimage = FileUpload1.PostedFile;
uploadimage.InputStream.Read
(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into image1(name,id,image)" + "values('" + TextBox1.Text + "','" + TextBox2.Text + "', @image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;


SqlParameter uploadImage = new SqlParameter("@image", SqlDbType.Image, imageSize.Length);
uploadImage.Value = imageSize;
cmd.Parameters.Add(uploadImage);

int result = cmd.ExecuteNonQuery();
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
FileUpload1.PostedFile.SaveAs(MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName).ToLower().ToString()));
Image1.ImageUrl = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName).ToLower().ToString();


}
img();
con.Close();
}
}
public void img()
{
string image_user, imagepath;
if (FileUpload1.HasFile)
{
image_user = FileUpload1.FileName.ToString().Trim();
imagepath = "image\\" + image_user;

FileUpload1.SaveAs(Server.MapPath(imagepath));


string filename = System.IO.Path.GetFileName(imagepath);
string ext = System.IO.Path.GetExtension(filename);
string contenttype = string.Empty; ;


if ((ext == ".JPEG") || (ext == ".jpg") || (ext == ".gif") || (ext == ".png"))
{
FileUpload1.SaveAs(Server.MapPath(imagepath));
Image1.ImageUrl = "image/" + image_user;
Label4.Text = image_user;
Image1.ImageUrl = FileUpload1.FileName.ToString().Trim();
}
else
{

Label6.Text = "<br><font color=red>" + "insert in proper format" + "</font>";
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from image1 where id='"+TextBox2.Text+"' " , con );
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from image1 where id='" + TextBox2.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
TextBox1.Text = dr["name"].ToString();
FileUpload1.Load = dr["image"].ToString();
cmd.ExecuteNonQuery();
con.Close();
}
}
use these code..
 
Share this answer
 
You can Do this in Different ways,
1).If we have the URLs of the Images in our table(ICollection) to which we binding the GridView we can directly use that column name to bind the data to GridView, By using
ASP.NET
<asp:imagefield xmlns:asp="#unknown"></asp:imagefield>
as shown
ASP.NET
<asp:imagefield alternatetext="no image" headertext="Image" dataimageurlfield="image" xmlns:asp="#unknown">
</asp:imagefield>

or if you dont have that collection(image collection) to which you are bonding, we can use
ASP.NET
<asp:imag xmlns:asp="#unknown" />
tag and use OnDataBonding event to set ImageURL as shown
ASP.NET
<asp:templatefield xmlns:asp="#unknown">
                <itemtemplate>
                <asp:image id="image" runat="server" />
                </itemtemplate>
</asp:templatefield>

and code behind
C#
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               Image i = (Image)e.Row.FindControl("image");
               i.ImageUrl = "wait.gif";
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900