当前位置:编程学习 > C#/ASP.NET >>

Winform DataGridView图片列显示重复的图片



datagridview图片列后台添加

现在查询前三行数据,第一行和第二行的图片是存在的,第三行的图片由于服务器没有此图片,就给了一个默认图片,现在for循环后,第一行和第二行的图片都会被默认的图片所覆盖,不知道原因,求教!!!

代码如下:
 public void Bind(string state)
        {
            string picname = "";
            SqlConnection conn = new SqlConnection(constring);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = null;
            if (state == "1dd")
            {
                adp = new SqlDataAdapter("select top 3 * from product", conn);
            }
            else if (state != "1dd")
            {
                adp = new SqlDataAdapter("select * from Product where ShortTitle  like '%" + state + "%'", conn);
                gdvPorductList.Columns.Remove("img");

            }
            adp.Fill(dt);
            this.gdvPorductList.RowTemplate.Height = 100;
            this.gdvPorductList.DataSource = dt;

            DataGridViewTextBoxColumn dgvc = (DataGridViewTextBoxColumn)this.gdvPorductList.Columns["ID"];
            DataGridViewImageColumn column = new DataGridViewImageColumn();
            column.Name = "img";
            // gdvPorductList.Columns.Add(column);
            gdvPorductList.Columns.Insert(2, column);
            column.HeaderText = "img";
            SqlConnection cn = new SqlConnection(constring);
            if (state == "1dd")
            {
              
            //   this.gdvPorductList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;//指定dataGridView的Column和Row的自动大小模式.
          //     this.gdvPorductList.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            //   column.ImageLayout = DataGridViewImageCellLayout.Zoom;//使图片自动适应单元格大小

                for (int a = 0; a < dgvc.DataGridView.Rows.Count; a++)
                {
                    try
                    {
                        string strSQL = "select Name from dbo.Img where state=1 and ProductID=" + dgvc.DataGridView.Rows[a].Cells["ID"].FormattedValue;

                        SqlCommand cmd = new SqlCommand(strSQL, cn);
                        cn.Open();
                        string name = (string)cmd.ExecuteScalar();
                     
                        if (name == null) //代表没有图片,可以用默认的
                        {
                            picname = "C://Users//IT3//Desktop//no_pic.jpg";
                            // dgvcc.Value = Image.FromFile(picname);
                            // string a2 = Application.StartupPath + @"\no_pic.jpg";
                           column.Image = System.Drawing.Image.FromFile(picname);

                           ((DataGridViewImageCell)this.gdvPorductList.Rows[a].Cells["img"]).Value = column.Image;

                        }
                        else
                        {
                            try
                            {
                                picname = name;
                              //  string path = AppDomain.CurrentDomain.BaseDirectory + picname;
                                string urlfilename = @"" + picname + "";
                                string replacename=picname.Replace("/","\\");
                                string[] sp = urlfilename.Split(new string[] { @"/", @"\" }, StringSplitOptions.RemoveEmptyEntries);
                                string yearmonthDir = sp[0];
                                string filename = sp[1];
                                string localfileDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "img", yearmonthDir);
                                if (!Directory.Exists(localfileDir))//说明:
                                {
                                    Directory.CreateDirectory(localfileDir);
                                }
                                string picname1 = urlWeb + "Images/Product/Adv/" + picname;
                                // string filepath = @"C:\" + name;
                                string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "img", name);
                                WebClient mywebclient = new WebClient();
                                string pathname = "";

                                mywebclient.DownloadFile(picname1, filepath);
                                pathname = @"" + AppDomain.CurrentDomain.BaseDirectory + @"img\" + replacename + "";

                                //   column.Image = System.Drawing.Image.FromFile(urlWeb + "\\Images\\Product\\Adv\\" + name);
                               
                                column.Image = System.Drawing.Image.FromFile(pathname);
                           
                                ((DataGridViewImageCell)this.gdvPorductList.Rows[a].Cells["img"]).Value = column.Image;
                               
                            
                            }
                            catch (Exception)
                            {
                                string pathname1 = "C://Users//IT3//Desktop//no_pic.jpg";
                                column.Image = System.Drawing.Image.FromFile(pathname1);
                               
                                ((DataGridViewImageCell)this.gdvPorductList.Rows[a].Cells["img"]).Value = column.Image;
                                if (((DataGridViewImageCell)this.gdvPorductList.Rows[1].Cells["img"]).Value == ((DataGridViewImageCell)this.gdvPorductList.Rows[2].Cells["img"]).Value)
                                {
                                    string adaf = "1";
                                }else
                                {
                                    string adaf = "12";
                                }
                                throw;
                            }
                        }
                    }
                    catch { 
                    
                    
                    }
                    cn.Close();
                }
            }
            
        //    this.gdvPorductList.DataSource = dt;

        }


winform datagridview --------------------编程问答-------------------- 你在前端用这种方法处理图片显示,感觉不好,应该以返回数据端未绑定时,先检查某一行是否有图片,没有就读取默认图片,或者直接在数据库的查询语句里面做好返回图片
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,