明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

  • 单元格中同时显示文本和图标

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;
            }
        }
    }

}

复制代码
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2006-11-26 恢复误删数据(SQL Server 2000)--Log Explorer
点击右上角即可分享
微信分享提示