天天看點

Winform DataGridView中利用WebClient異步加載顯示網絡位址的圖檔

Winform DataGridView中利用WebClient異步加載顯示網絡位址的圖檔

2008年11月21日 浮出水面者 我要蓋樓

Winform中的DataGridView,支援顯示圖檔的一種列類型(Column Type),叫 DataGridViewImageColumn ,顯示圖檔就是用這種列,但是這種列不支援網絡位址,要顯示網絡上的圖檔,必須下載下傳到本地,由于一個datagridview中顯示的資料量可能比較大,如果每行的圖檔都是同步顯示,則程式會長時間的BLOCK住,UE會很差,是以需要采用異步加載的方式。

Winform DataGridView中利用WebClient異步加載顯示網絡位址的圖檔
/*--------------------------------------
 *
 * Coding By DeltaCat
 *
 * http://www.zu14.cn
 *
 * 2008.11.21
 *
 --------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Windows.Forms;

namespace DataGridView_WebImageColumn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 手動添加模拟資料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;

            Random rnd = new Random();

            手動添加5條測試資料
            for (int i = 1; i < 6; i++)
            {
                this.dataGridView1.Rows.Add(i.ToString(), "産品" + i.ToString(),
                    string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "¥{0:#.00}",
                    rnd.NextDouble() * 10000),
                    null);
            }
        }

        /// <summary>
        /// 處理dataGridView1的RowsAdded事件,在每行被載入後,即開始異步擷取圖檔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            利用 WebClient 來下載下傳圖檔
            using (WebClient wc = new WebClient())
            {
                WebClient 下載下傳完畢的響應事件綁定
                wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);

                開始異步下載下傳,圖檔URL路徑請根據實際情況自己去指定
                同時将DataGridView目前行的行号傳遞過去,用于指定圖檔顯示的CELL
                wc.DownloadDataAsync(new Uri(string.Format("http://www.zu14.cn/tip/{0}.gif", e.RowIndex + 5)),
                    e.RowIndex);
            }
        }

        /// <summary>
        /// 圖檔下載下傳完畢,顯示于對應的CELL
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            如果下載下傳過程未發生錯誤,并且未被中途取消
            if (e.Error == null && !e.Cancelled)
            {
                将圖檔顯示于對應的指定單元格, e.UserState 就是傳入的 e.RowIndex
                e.Result 就是下載下傳結果
                this.dataGridView1.Rows[(int)e.UserState].Cells["PictureColumn"].Value = e.Result;
            }
        }
    }
}      

源程式下載下傳 DataGridViewImageColumn雖然支援顯示圖檔,但預設情況下,不支援GIF動畫,動畫GIF圖檔,隻顯示第一幀,要顯示動畫圖檔,需做附加處理,後續…

繼續閱讀