Start New Discussion within our Software Development Community

hello !

i am new in c# , i have images in my mssql table and i want to show those images in the datagridview , i add a column type image , but i am not able to show images in it , please help me in this .

Regards

Hi, fill DataTable with images from database (or where ever), an then do a loop throuh the rows of dgv, and paste image into each row (to each dgv_image_cell):

  //create image column
  DataGridViewImageColumn ic= new DataGridViewImageColumn();
  ic.HeaderText = "Images";
  ic.Image = null;
  ic.Name = "imageColumn";
  ic.Width = 100;
  dataGridView1.Columns.Add(ic);

 //then fill DataTable 
 //NOTE: Images should be in bytes saved into database!!!

 DataTable table = new DataTable();
 using(SqlConnection sqlcon = new SqlConnection("connString"))
 {      
     string strquery = "SELECT MyImageColumn FROM MyTable";  
     using(SqlDataAdapter da = new SqlDataAdapter(strquery, sqlcon))
     {
        da.Fill(table);
     }
 }
  //1.
  //now lets loop through the rows of dgv and add images from dataTable!
  //NOTE: BE CAREFUL on the number of rows in DGV, and in DataTable!!!
  //If they are not the same, create some conditions!!

  for(int i = 0; i < dataGridView1.Rows.Count; i++)
  {
      DataGridViewImageCell cell = table.Rows[i][ColumnName"] as DataGridViewImageCell;
      dataGridView1["imageColumn", i].Value = cell;
  }

  //OR:
  //2.
  //Lets loop through the rows of DataTable!
  //Be aware of the same issue as above - number of rows in both collections! 
  for(int i = 0; i < table.Rows.Count; i++)
  {
      DataGridViewImageCell cell = table.Rows[i][ColumnName"] as DataGridViewImageCell;
      dataGridView1["imageColumn", i].Value = cell;
  }

Hope it helps,bye

This article has been dead for over six months. Start a new discussion instead.