-
大小: 31KB文件类型: .rar金币: 2下载: 0 次发布日期: 2021-06-01
- 语言: C#
- 标签: DataGridView WebClient 异步加载
资源简介
Winform中的DataGridView,支持显示图片的一种列类型(Column Type),叫 DataGridViewImageColumn ,显示图片就是用这种列,但是这种列不支持网络地址,要显示网络上的图片,必须下载到本地,由于一个datagridview中显示的数据量可能比较大,如果每行的图片都是同步显示,则程序会长时间的BLOCK住,UE会很差,所以需要采用异步加载的方式。

代码片段和文件信息
/*--------------------------------------
*
* 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();
}
///
/// 手动添加模拟数据
///
///
///
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);
}
}
///
/// 处理dataGridView1的RowsAdded事件,在每行被载入后,即开始异步获取图片
///
///
///
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);
}
}
///
/// 图片下载完毕,显示于对应的CELL
///
///
///
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;
}
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6574 2008-11-21 11:00 Form1.resx
文件 497 2008-11-21 09:57 Program.cs
文件 24576 2008-11-21 14:09 bin\Debug\DataGridView_WebImageColumn.exe
文件 26112 2008-11-21 14:09 bin\Debug\DataGridView_WebImageColumn.pdb
文件 5632 2008-11-21 14:09 bin\Debug\DataGridView_WebImageColumn.vshost.exe
文件 1072 2008-11-21 14:09 obj\DataGridView_WebImageColumn.csproj.FileListAbsolute.txt
文件 842 2008-11-21 11:00 obj\Debug\DataGridView_WebImageColumn.csproj.GenerateResource.Cache
文件 24576 2008-11-21 14:09 obj\Debug\DataGridView_WebImageColumn.exe
文件 180 2008-11-21 11:00 obj\Debug\DataGridView_WebImageColumn.Form1.resources
文件 26112 2008-11-21 14:09 obj\Debug\DataGridView_WebImageColumn.pdb
文件 180 2008-11-21 10:09 obj\Debug\DataGridView_WebImageColumn.Properties.Resources.resources
文件 1312 2008-11-21 09:57 Properties\AssemblyInfo.cs
文件 2883 2008-11-21 09:57 Properties\Resources.Designer.cs
文件 5612 2008-11-21 09:57 Properties\Resources.resx
文件 1112 2008-11-21 09:57 Properties\Settings.Designer.cs
文件 249 2008-11-21 09:57 Properties\Settings.settings
文件 3261 2008-11-21 14:08 DataGridView_WebImageColumn.csproj
文件 942 2008-11-21 09:57 DataGridView_WebImageColumn.sln
文件 3022 2008-11-21 13:55 Form1.cs
文件 5312 2008-11-21 11:00 Form1.Designer.cs
目录 0 2008-11-21 09:57 obj\Debug\TempPE
目录 0 2008-11-21 10:09 bin\Debug
目录 0 2008-11-21 14:09 obj\Debug
目录 0 2008-11-21 09:57 bin
目录 0 2008-11-21 10:09 obj
目录 0 2008-11-21 09:57 Properties
----------- --------- ---------- ----- ----
140058 26
- 上一篇:C# 编写的员工管理信息系统
- 下一篇:asp.net 导入excel到数据库中的
相关资源
- datagridview控件美化
- c#DataGridView单元格合并
-
C#读取xm
l文件数据。显示在datagrid -
读取xm
l显示在dataGridView - excel转化成datatable 并加载到datagridvi
- c#实现可折叠展开的DataGridView提供dl
- (VB.NET)WinForm自定义DataGridView分页组
- 免费DataGridView打印及.NET轻松打印控件
- 免费DataGridView打印及.NET轻松打印控件
- C#访问SQL显示到DataGridView,同时根据
- DataGridView修改后直接保存到数据库程
- C# 导入excel到datagridview
- C#dataGridView中输入框自动提示、自动完
- 两个DataGridView通过委托实时传值,安
- TreeDataGridView
- C# winform datagridview 里的下拉框中 既可
- DataGridView列标头带数据筛选功能(含
- C#winform DataGridView checkbox复选框 实现
- C#_winform_DataGridView_checkbox复选框_实现
- C# DataGridView单元格中动态添加多个控
- Q758437.zip c#中如何将datagridview中的数据
- C#从Excel中提取数据到DataGridView中显示
- C# 从Excel导入数据到DataGridView、SqlSe
- C#对DataGridView进行添加、修改、删除数
- C# datagridview 与数据源绑定后对数据的
- c#窗体中的DataGridView及TreeView的应用
- winform(c#) DataGridView控件多维合并表
- winform DataGridView 多行及表头单元格的
- Datagridview分页控件
- winform高效率的分页查询
评论
共有 条评论