Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
How to display DataGridView Image Details winform.
Image

SQL Data Base
C#
ID:1001
Image(Photo):image
Name:aaa
Age:25
Gender:M
Time:9.30 am

VB
ID:1002
Image(Photo):image
Name:bbb
Age:26
Gender:M
Time:9.31 am

VB
ID:1003
Image(Photo):image
Name:ccc
Age:26
Gender:M
Time:9.30 am

In DataGridView Like that show
C#
1001 [Image] 25 M 
aaa       
9.30 am

C#
1002 [Image] 26 M
bbb
9.31 am

C#
1003 [Image] 26 M 
ccc         
>9.30 am

Regards,
Karthikeyan,
Bangalore.
Posted
Updated 15-Oct-12 4:40am
v3
Comments
I.explore.code 15-Oct-12 6:35am    
You mean you want to show the image thumbnail in DataGridView?
pkarthionline 15-Oct-12 7:04am    
yes
I.explore.code 15-Oct-12 7:10am    
Your question is misleading because you have tagged ASP.NET but you are talking about DataGridView and WinForms. You would have to make sure what you are looking for a solution for, none of the solutions posted here are for WinForms they would only work for ASP.NET.
pkarthionline 15-Oct-12 7:13am    
winforms c#.net only.
I.explore.code 15-Oct-12 7:24am    
I have posted a potential solution for WinForms. Have a look, its not a ready-made code but something for you to work with and improve on.

hi. . .

This will be useful to retrieve the image from database, to display in a gridview

XML
<asp:GridView runat="server" DataKeyNames="imageid" ID="Gridview1">

      <Columns>
         <asp:TemplateField HeaderText="Image">
            <ItemTemplate>

               <img src="ImageRetrive.aspx?imageid=<%# Eval("imageid") %>" height="200px" width="200px" alt="Available" />

            </ItemTemplate>
         </asp:TemplateField>
      </Columns>

      </asp:GridView>



Write the below in the page_load of imageRetrive.aspx


C#
protected void Page_Load(object sender, EventArgs e)
   {
       int imageid = int.Parse(Request.QueryString["imageid"].ToString());

       string ConnString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

       byte[] byeteArray = new byte[5000];

       using (SqlConnection con = new SqlConnection(ConnString))
       {
           using (SqlCommand cmd = new SqlCommand("select myimage from storeimages where imageid="+imageid, con))
           {


               con.Open();
               byeteArray = (byte[])cmd.ExecuteScalar();
               Response.BinaryWrite(byeteArray);
               con.Close();
           }
       }



   }
 
Share this answer
 
Comments
I.explore.code 15-Oct-12 7:08am    
I would recommend using an HttpHandler instead of an aspx page. Works better if there is no UI needed to be shown and only backend processing is required.
Samsani.v.s.Durga Prasad 15-Oct-12 7:19am    
There no ui in ImageRetrive.aspx. For getting the image ImageRetrive.aspx will be useful
I.explore.code 15-Oct-12 7:22am    
well as it turns out, the question is for Winforms and not for ASP.NET. Blame the OP ;)
Samsani.v.s.Durga Prasad 15-Oct-12 7:30am    
ok after try i will reply thanks
This is not the full solution but it will give you an idea to move in a direction. You are gonna have to do some work yourself to improvise this code, particularly to get a small size of the image to display as a thumbnail (that's an exercise for you).

C#
SqlConnection conn = new SqlConnection("connection string");
            conn.Open();
            string get = "select * from Table1";
            SqlDataAdapter da = new SqlDataAdapter();
            
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = get;
            cmd.Connection = conn;

            da.SelectCommand = cmd;
            DataSet ds = new System.Data.DataSet();
            da.Fill(ds);
            byte[] img = null;
//in my case the fourth column has an image
img = (byte[]) ds.Tables[0].Rows[0][3];
            MemoryStream ms = new MemoryStream(img);
//for some reason this does not seem be creating a thumbnail
            Image.GetThumbnailImageAbort cb = new Image.GetThumbnailImageAbort(ThumCallBack);
            Image thumb = Image.FromStream(ms).GetThumbnailImage(10, 10, cb, IntPtr.Zero);
//theoretically, save the thumbnail back to the memory stream we opened above
            thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//read it back into the img array
            ms.Read(img, 0, img.Length);
//put it back in the dataset before we actually bind the data to the DataGridView
            ds.Tables[0].Rows[0][3] = img;
            dataGridView1.DataSource = ds.Tables[0];
...
public bool ThumCallBack()
        {
            return true;
        }


Hope this helps a bit.
 
Share this answer
 
v2

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