循环从数据库里面读出图片然后放置到ListView中的例子

时间:2022-08-22 19:37:51
 
try
            {
                conn.Open();
                OleDbDataAdapter myDA = new OleDbDataAdapter(comStr, conn);
                OleDbDataAdapter myDAStandImg = new OleDbDataAdapter(comStrStandImg, conn);
                DataSet myDS = new DataSet();
                myDA.Fill(myDS, "ImgList");
                myDAStandImg.Fill(myDS, "StandImg");

                ImgListHV.Images.Clear();
                listViewImgList_HV.Items.Clear();
                for (int i = 0; i < myDS.Tables[0].Rows.Count; i++)
                {
                    //定义Image循环往ImageList中添加图片
                    Image myImage = Image.FromFile(@".\image\" + myDS.Tables[0].Rows[i][0].ToString());
                    ImgListHV.Images.Add(myImage);
                    //向ListView中添加Item
                    ListViewItem subLVI = new ListViewItem();
                    subLVI.ImageIndex = i;
                    //把路径存贮在Tag属性里,方便以后使用
                    subLVI.Tag = ".\\image\\" + myDS.Tables[0].Rows[i][0].ToString();
                    listViewImgList_HV.Items.Add(subLVI);
                    myImage.Dispose();
                }
                Image StandImg = Image.FromFile(@".\image\" + myDS.Tables["StandImg"].Rows[0][0].ToString());
                picBoxStandardImg_HV.Image = StandImg;
            }