Winform DataGridView列的单元格中动态添加图片和文字

本文介绍如何在Winform应用的DataGridView控件中动态添加图片和文字。通过重写DataGridViewTextBoxColumn类,创建TextAndImageColumn.cs,然后在Form1.cs中设置 DataGridView,展示带有图片和文字的单元格。

先上图在说,第二列中图片和文字的样式

1、需要重写DataGridViewTextBoxColumn,新建类TextAndImageColumn.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows.Forms;
  6 using System.Drawing;
  7 
  8 namespace DataGridViewTest
  9 {
 10     public class TextAndImageColumn : DataGridViewTextBoxColumn
 11     {
 12         private Image imageValue;
 13         private Size imageSize;
 14 
 15         public TextAndImageColumn()
 16         {
 17             this.CellTemplate = new TextAndImageCell();
 18         }
 19 
 20         public override object Clone()
 21         {
 22             TextAndImageColumn c = base.Clone() as TextAndImageColumn;
 23             c.imageValue = this.imageValue;
 24             c.imageSize = this.imageSize;
 25             return c;
 26         }
 27 
 28         public Image Image
 29         {
 30             get { return this.imageValue; }
 31             set
 32             {
 33                 if (this.Image != value)
 34                 {
 35                     this.imageValue = value;
 36                     this.imageSize = value.Size;
 37 
 38                     if (this.InheritedStyle != null)
 39                     {
 40                         Padding inheritedPadding = this.InheritedStyle.Padding;
 41                         this.DefaultCellStyle.Padding = new Padding(imageSize.Width,
 42                     inheritedPadding.Top, inheritedPadding.Right,
 43                     inheritedPadding.Bottom);
 44                     }
 45                 }
 46             }
 47         }
 48         private TextAndImageCell TextAndImageCellTemplate
 49         {
 50             get { return this.CellTemplate as TextAndImage
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值