在Winform的DataGridView的单元格中同时显示文本和图标,以及树形结构的示例
2010-07-12 17:22
525 查看
树形结构的DataGridView:Customizing the DataGridView to support expanding/collapsing (ala TreeGridView) DataGridView
TreeGridView 修正版本:Some people reported problems actually running the TreeGridView sample, so I've recompiled it on an RTM build machine (previously I was using the latest internal VS build). You can get the new version (that also includes release & debug binaries prebuilt) here: http://www.windowsforms.net/blogs/markrideout/TreeGridViewRebuilt.zip
单元格中同时显示文本和图标
The DataGridView control does not have any built-in support for showing an icon and text in the same cell. Through the different painting customization events, such as the CellPaint event, you can easily display an icon next to the text in the cell.
The following example extends the DataGridViewTextColumn and cell to paint an image next to the text. The sample uses the DataGridViewCellStyle.Padding property to adjust the text location and overrides the Paint method to paint an icon. This sample can be simplified by handling the CellPainting event and performing similar code.
TreeGridView 修正版本:Some people reported problems actually running the TreeGridView sample, so I've recompiled it on an RTM build machine (previously I was using the latest internal VS build). You can get the new version (that also includes release & debug binaries prebuilt) here: http://www.windowsforms.net/blogs/markrideout/TreeGridViewRebuilt.zip
单元格中同时显示文本和图标
The DataGridView control does not have any built-in support for showing an icon and text in the same cell. Through the different painting customization events, such as the CellPaint event, you can easily display an icon next to the text in the cell.
The following example extends the DataGridViewTextColumn and cell to paint an image next to the text. The sample uses the DataGridViewCellStyle.Padding property to adjust the text location and overrides the Paint method to paint an icon. This sample can be simplified by handling the CellPainting event and performing similar code.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace JrtGenerator.Utility { 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; } } } }
相关文章推荐
- 在Winform的DataGridView的单元格中同时显示文本和图标,以及树形结构的示例
- 在Winform的DataGridView的单元格中同时显示文本和图标,以及树形结构的示例
- 在Winform的DataGridView的单元格中同时显示文本和图标,以及树形结构的示例
- C# Winform datagridview 在一个单元格中 同时存在文本和按钮
- WinForm----DataGridview---连接数据库,以及双击一条数据,显示信息到Label控件,也可以是TextBox控件。
- WinForm——DataGridView单元格文本自动换行
- C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)
- [转]动态更新Toolbar Menu以及Menu中同时显示文字和图标
- [WinForm]- 设置DataGridView单元格内根据不同值显示图片
- C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)
- WinForm系列——DataGridView单元格文本自动换行
- C#Winform基础 显示选中行的第一列单元格的内容,datagridview控件
- ReactJs实现树形结构的数据显示的组件的示例
- IE Web Control介绍以及TreeView 控件树形结构不能显示的问题
- C# Winform中让DataGridView单元格显示图片
- C#Winform基础 显示选中行的第一列单元格的内容,datagridview控件
- winform datagridview 添加按钮列 行为空时按钮列显示文本
- C# Winform中让DataGridView单元格显示图片
- 关于C# Winform dataGridView显示时没有默认选中以及dataGridView添加DataTable中的数据
- 关于是的datagridview根据数据源显示自己想要的对应文本内容