你的位置:首页 > 软件开发 > ASP.net > Winform DataGridView列的单元格中动态添加图片和文字

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

发布时间:2015-12-06 21:00:16
先上图在说,第二列中图片和文字的样式1、需要重写DataGridViewTextBoxColumn,新建类TextAndImageColumn.cs 1 using System; 2 using System.Collections.Generic; 3 using Syste ...

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

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

1、需要重写Dataget='_blank'>GridViewTextBoxColumn,新建类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 TextAndImageCell; } 51     } 52     internal Size ImageSize 53     { 54       get { return imageSize; } 55     } 56   } 57  58   public class TextAndImageCell : DataGridViewTextBoxCell 59   { 60     private Image imageValue; 61     private Size imageSize; 62  63     public override object Clone() 64     { 65       TextAndImageCell c = base.Clone() as TextAndImageCell; 66       c.imageValue = this.imageValue; 67       c.imageSize = this.imageSize; 68       return c; 69     } 70  71     public Image Image 72     { 73       get 74       { 75         if (this.OwningColumn == null || 76      this.OwningTextAndImageColumn == null) 77         { 78  79           return imageValue; 80         } 81         else if (this.imageValue != null) 82         { 83           return this.imageValue; 84         } 85         else 86         { 87           return this.OwningTextAndImageColumn.Image; 88         } 89       } 90       set 91       { 92         if (this.imageValue != value) 93         { 94           this.imageValue = value; 95           this.imageSize = value.Size; 96  97           Padding inheritedPadding = this.InheritedStyle.Padding; 98           this.Style.Padding = new Padding(imageSize.Width, 99           inheritedPadding.Top, inheritedPadding.Right,100           inheritedPadding.Bottom);101         }102       }103     }104     protected override void Paint(Graphics graphics, Rectangle clipBounds,105    Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,106    object value, object formattedValue, string errorText,107     DataGridViewCellStyle cellStyle,108     DataGridViewAdvancedBorderStyle advancedBorderStyle,109     DataGridViewPaintParts paintParts)110     {111       // Paint the base content112       base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,113        value, formattedValue, errorText, cellStyle,114        advancedBorderStyle, paintParts);115 116       if (this.Image != null)117       {118         // Draw the image clipped to the cell.119         System.Drawing.Drawing2D.GraphicsContainer container =120         graphics.BeginContainer();121 122         graphics.SetClip(cellBounds);123         graphics.DrawImageUnscaled(this.Image, cellBounds.Location);124 125         graphics.EndContainer(container);126       }127     }128 129     private TextAndImageColumn OwningTextAndImageColumn130     {131       get { return this.OwningColumn as TextAndImageColumn; }132     }133   }134 }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

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

关键词:GridView

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。