winform .net如果要在datagridview中显示图像,最好的方法

时间:2009-12-15 10:37:14

标签: winforms datagridview picturebox

net webdeveloper,通常不会制作任何win32应用。但现在我必须。我有一个约2000条目的列表。 每个条目应显示为带有文本框的标签,另一个标签和图片。我使用了flowlayoutpanel做了这个,我在条目上做了一个foreach,为每个条目制作一个带有标签,文本框,标签和图片框的面板。

现在我有超过1000个条目的渲染问题。 所以我读过我应该使用listview或datagridview。

现在我有一个像这样的datagridview:

DataGridView dgv = new DataGridView();
dgv.AutoSize = true;
dgv.ScrollBars = ScrollBars.Vertical;

System.Data.DataTable dt = new System.Data.DataTable();
DataColumn dc1 = new DataColumn("Code", typeof(string));
dc1.ReadOnly = true;
dt.Columns.Add(dc1);
dt.Columns.Add(new DataColumn("Quantity", typeof(int)));
DataColumn dc3 = new DataColumn("Price", typeof(string));
dc3.ReadOnly = true;
dt.Columns.Add(dc3);
dt.Columns.Add(new DataColumn("Image", typeof(Bitmap)));

foreach (Product pd in products)
{
      DataRow dr = dt.NewRow();
      dr["Code"] = pd.ProductCode;
      dr["Quantity"] = pd.ProductQuantity;
      dr["Price"] = "€ " + String.Format("{0:0,00}", pd.ProductResalePrice.ToString());

      dr["Image"] = BitmapFromWeb(pd.ProductImage);
      dt.Rows.Add(dr);
}

dt.AcceptChanges();
dgv.RowTemplate.Height = 50;
dgv.DataSource = dt;

但问题是数据网格视图上的位图真的很慢!我之前拥有的图片框选项和面板要快得多。我该如何解决这个问题?

第二个问题是:当我想跟踪第二列中所做的更改时,我需要哪个事件?

有一点:图像在线可用,因此'pd.ProductImage'是一个网址

    private static Bitmap BitmapFromWeb(string URL)
    {
        try
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.Method = "GET";
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();

            return bmp;
        }
        catch (Exception ex)
        {
            return null; // if for some reason we couldn't get to image, we return null
        }
    }

2 个答案:

答案 0 :(得分:4)

异步加载图像然后强制刷新单元格。您可以将foreach代码放入对ThreadPool的调用中,例如......

ThreadPool.QueueUserWorkItem(delegate
{
 foreach (DataRow row in dt)
 {
  row["Image"] = BitmapFromWeb(Products[row["Code"]].ProductImage);
  //maybe a call to invalidate here, remember to do Control.Invoke(...)
 }
}

编辑:这是我在Form构造函数中测试的示例代码...

        DataTable t= new DataTable();
        t.Columns.Add("id");
        t.Columns.Add("uri");
        t.Columns.Add(new DataColumn("Img",typeof(Bitmap)));

        Bitmap b = new Bitmap(50, 15);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.DrawString("Loading...", this.Font, new SolidBrush(Color.Black), 0f,0f);
        }

        t.Rows.Add(new object[] { "1", "http://farm1.static.flickr.com/88/377522544_c4774f15cc_s.jpg", b });
        t.Rows.Add(new object[] { "2", "http://farm1.static.flickr.com/175/377522405_2c505def99_s.jpg", b });
        t.Rows.Add(new object[] { "3", "http://farm1.static.flickr.com/185/377524902_72f82e2db9_s.jpg", b });
        t.Rows.Add(new object[] { "4", "http://farm1.static.flickr.com/136/377524944_d011abf786_s.jpg", b });
        t.Rows.Add(new object[] { "5", "http://farm1.static.flickr.com/137/377528675_d3b9d541fb_s.jpg", b });
        dataGridView1.DataSource = t;
        ThreadPool.QueueUserWorkItem(delegate
        {
            foreach (DataRow row in t.Rows)
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["uri"].ToString());
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
                myResponse.Close();

                row["Img"] = bmp;
            }
        });

        dataGridView1.CellEndEdit += dataGridView1_CellEndEdit;

....并在单元格末尾编辑代码:

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string value = dataGridView1.Rows[e.RowIndex].Cells["uri"].Value.ToString();
        ThreadPool.QueueUserWorkItem(delegate
        {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(value);
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
                myResponse.Close();
                dataGridView1.Rows[e.RowIndex].Cells["Img"].Value=bmp;
        });
    }

答案 1 :(得分:1)

// table is DataTable object declared as member in my form class
table = new DataTable();
table.Columns.Add(new DataColumn("Column1", typeof(string)));
table.Columns.Add(new DataColumn("Column2", typeof(string)));
table.Columns.Add(new DataColumn("Column3", typeof(System.Drawing.Bitmap)));

dataGridView1.DataSource = table;