没有合适的资源?快使用搜索试试~ 我知道了~
DataGridView中同一个Cell中显示图片和文字
4星 · 超过85%的资源 需积分: 50 125 下载量 16 浏览量
2013-04-03
17:33:05
上传
评论 3
收藏 5KB TXT 举报
温馨提示
试读
3页
在DataGridView中,通过CellPainting事件来重新绘制单元格,来实现同一个单元格中显示图片和文字。
资源推荐
资源详情
资源评论
要在同一个单元格中显示图片和文字,在网上找了很久,总的来说有这两种方法,第一种就是进行控件的扩展,http://suponji.blog.163.com/blog/static/1723790302011231539809/里面讲的很清楚(没有实际操作过,所以不知道效果如何),第二种方法就是用DataGridView中的CellPainting事件来进行单元格的重新绘制(网上很多人用这个事件处理单元格的合并),用这种方法需要注意几个问题,代码如下:
private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == 2)
{
if (this.dgvData.Rows[e.RowIndex].Cells["file_Extension"].Value == DBNull.Value)
return;
string strExtension = this.dgvData.Rows[e.RowIndex].Cells["file_Extension"].Value.ToString();
string strFileName = this.dgvData.Rows[e.RowIndex].Cells["file_Name"].Value.ToString();
Image image;
string imagePath = Application.StartupPath;
if (strExtension == "doc" || strExtension == "docx")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\doc.gif");
}
else if (strExtension == "txt")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\txt.gif");
}
else if (strExtension == "xls" || strExtension == "xlsx")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\xls.gif");
}
else
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\wz.bmp");
}
//定义显示图片的矩形
Rectangle newRect = new Rectangle(e.CellBounds.X + 2, e.CellBounds.Y + 2, e.CellBounds.Height - 6,
e.CellBounds.Height - 6);
private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == 2)
{
if (this.dgvData.Rows[e.RowIndex].Cells["file_Extension"].Value == DBNull.Value)
return;
string strExtension = this.dgvData.Rows[e.RowIndex].Cells["file_Extension"].Value.ToString();
string strFileName = this.dgvData.Rows[e.RowIndex].Cells["file_Name"].Value.ToString();
Image image;
string imagePath = Application.StartupPath;
if (strExtension == "doc" || strExtension == "docx")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\doc.gif");
}
else if (strExtension == "txt")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\txt.gif");
}
else if (strExtension == "xls" || strExtension == "xlsx")
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\xls.gif");
}
else
{
image = Image.FromFile(imagePath + "\\Images\\FileImage\\wz.bmp");
}
//定义显示图片的矩形
Rectangle newRect = new Rectangle(e.CellBounds.X + 2, e.CellBounds.Y + 2, e.CellBounds.Height - 6,
e.CellBounds.Height - 6);
Kim_Du
- 粉丝: 10
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页