排序

时间:2015-08-18 15:23:06

标签: datagridview datagridviewimagecolumn

我有一个 DataGridView ,其中包含image列。 image地址从同一行的一个单元格中读取,然后显示。

当我对列进行排序时,图像会消失。

我正在使用Visual Studio 2010并在 C#中编码。使用 oledb 方法从anaccess数据库中检索数据。

填写image列的代码如下:

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;

我该如何解决?

谢谢

1 个答案:

答案 0 :(得分:0)

我知道这很老,但是今天早上我为此感到困扰。对dataviewgrid排序时,在运行时添加的所有图像都将丢失。内置的DataGridView排序功能可以解决此问题。

示例

DGV as Bound >> DGV after sorting

对我来说,我刚刚将图像添加代码添加到了DGV。Sorted事件中。这样做有点不对劲,但是我不知道为什么这些图像首先会丢失。

  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;
            }           
    }
相关问题