DataGridView图像文本单元格
来源:互联网 发布:华泰证券软件下载 编辑:程序博客网 时间:2024/06/06 13:56
为了美观,有时候我们需要文本单元格中带有图标,然而微软自带的DataGridView控件中没有类似的控件。下面的示例就是实现这样的一个控件。在文本前显示图像,显示出的图像如果太大,效果不好看,可以结合之前写过的C#图像缩放与裁剪,自己加以修改整合。
部分代码:
public class TextAndImageColumn : DataGridViewTextBoxColumn { private Image imageValue; private Size imageSize; public TextAndImageColumn() { this.CellTemplate = new TextAndImageCell(); } public override object Clone() { TextAndImageColumn c = base.Clone() as TextAndImageColumn; c.imageValue = this.imageValue; c.imageSize = this.imageSize; return c; } public Image Image { get { return this.imageValue; } set { if (this.Image != value) { this.imageValue = value; this.imageSize = value.Size; if (this.InheritedStyle != null) { Padding inheritedPadding = this.InheritedStyle.Padding; this.DefaultCellStyle.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom); } } } } private TextAndImageCell TextAndImageCellTemplate { get { return this.CellTemplate as TextAndImageCell; } } internal Size ImageSize { get { return imageSize; } } } public class TextAndImageCell : DataGridViewTextBoxCell { private Image imageValue; private Size imageSize; public override object Clone() { TextAndImageCell c = base.Clone() as TextAndImageCell; c.imageValue = this.imageValue; c.imageSize = this.imageSize; return c; } public Image Image { get { if (this.OwningColumn == null || this.OwningTextAndImageColumn == null) { return imageValue; } else if (this.imageValue != null) { return this.imageValue; } else { return this.OwningTextAndImageColumn.Image; } } set { if (this.imageValue != value) { this.imageValue = value; this.imageSize = value.Size; Padding inheritedPadding = this.InheritedStyle.Padding; this.Style.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom); } } } protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // Paint the base content base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); if (this.Image != null) { // Draw the image clipped to the cell. System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer(); graphics.SetClip(cellBounds); graphics.DrawImageUnscaled(this.Image, cellBounds.Location); graphics.EndContainer(container); } } private TextAndImageColumn OwningTextAndImageColumn { get { return this.OwningColumn as TextAndImageColumn; } } }
转载请注明出处!!!
http://blog.csdn.net/stuqbx
- DataGridView图像文本单元格
- datagridview 文本和图像列类
- WinForm系列——DataGridView单元格文本自动换行
- [转]C#控件——DataGridView单元格文本自动换行
- DataGridView合并单元格 编辑单元格
- DataGridView使用技巧—获取任意行或列的单元格文本
- C#Winfrom设置DataGridView单元格文本格式为钱的格式
- C# Winform datagridview 在一个单元格中 同时存在文本和按钮
- datagridview单元格数据验证
- DataGridView单元格代码控制
- DataGridView 单元格验证{转}
- DataGridView 单元格之间计算
- datagridview单元格数据验证
- datagridview 合并单元格
- DataGridView 合并单元格
- datagridview单元格合并
- 修改dataGridView的单元格
- datagridview单元格颜色赋值
- Rman备份中常见的问题
- redhat/centos-6.3上opennebula-3.8部署
- 将16进制的颜色值变成UIColor
- 关键段,事件,互斥量,信号量的“遗弃”问题
- Quartz 各种绘制图形用法 Quartz 实现画图片、写文字、画线、椭圆、矩形、棱形等。
- DataGridView图像文本单元格
- 肖特基二极管
- Cocos2d触摸分发原理
- 献给不知道自己该干什么的朋友—值得一看
- POJ 1011 (DFS+剪枝)
- 字符串
- Easyui自定义validate验证
- oracle解锁
- 关于Filter中的两个异常,重定向后要加Return