Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I am working on windows form c# and I want display images from database in gridview But i can't do this in so many tries.

C#
private void GetData(string selectCommand)
        {
   try
   {
String connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ODYSSEY\Downloads\Documents\Panorama
\MyImages.mdf;
Integrated Security=True;User Instance=True";
 
SqlDataAdapter adap = new SqlDataAdapter(selectCommand, connectionString);

SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adap);
DataTable table = new DataTable();
 table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adap.Fill(table);
               dataGridView1.DataSource = table;
                dataGridView1.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("Error to load data from database, Please check it.....");
            }
        }

 private void viewall_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = dataGridView1;
GetData("select ImageData from ImageData");
}


it show rows but it do not display images in rows...
Posted
Updated 22-Sep-12 4:13am
v3
Comments
[no name] 22-Sep-12 10:15am    
Try setting the datasource after you actually get the data.
Yasir Farooq 22-Sep-12 10:31am    
I want to show only images so i make 1 column in gridview that is image column, when i run my program then in gridview show same number of rows as in database table. but in all rows show default cross image sign.
[no name] 22-Sep-12 10:40am    
That is because you are not limiting the number of images you are getting from the database and you are not converting the image data back into an image.
Yasir Farooq 22-Sep-12 11:04am    
So You help me how i convert Binary image data into image?

1 solution

I once did something similar, but all from the interface builder using databinding, when I did it that way it automatically detected the images and displayed them.

A few small questions: does it show the column with the correct name for the images, what datatype do your images have in the database, does it work when you do it completely from the interface builder.

If it does show the column, and it has the correct name, maybe you simply need to a row height for the image to show.

Please post a reply if you need further help.

PS. As a small extra tip, take a look at app.config files. Those are ideal for storing settings like database connection strings: http://generally.wordpress.com/2007/09/27/using-appconfig-for-user-defined-runtime-parameters/[^]
 
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