Reputation: 21
I have a DataGridView that has an image
column. The image
address is read from one of the cells of the same row and then shown.
When I sort the columns the images go away.
I'm using Visual Studio 2010 and coding in C#. Data is retrieved from anaccess database using oledb methods.
Code that fills image
columns is below:
Image Image_File = Image.FromFile("d:\\2.jpg");
DataGridViewAutoSizeColumnMode.Fill;
for (int i=0;i < info_Grid.RowCount;i++)
{
if (Info_Grid.Rows[i].Cells[6].Value !=" ")
{
Image_File = Image.FromFile(Info_Grid.Rows[i].Cells[5].Value.ToString());
Info_Grid.Rows[i].Cells["Image_Col"].Value = Image_File;
}
}
Info_Grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
How can I fix it?
Thanks
Upvotes: 2
Views: 1103
Reputation: 1803
I know this is old, but I was troubled by this this morning. When sorting a dataviewgrid, all the images added at runtime are lost. Something about the built-in DataGridView sorting breaks this.
Example
For me, I just added my image add code to the DGV.Sorted
event. It somehow feels wrong to have to do this, but I do not know why these images are lost on sort in the first place.
private void dgvChecks_Sorted(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgvChecks.Rows)
{
if (row.Cells["Valid"].Value.ToString() == "True")
row.Cells[0].Value = (System.Drawing.Image)Properties.Resources.check;
else
row.Cells[0].Value = (System.Drawing.Image)Properties.Resources.delete;
}
}
Upvotes: 1