DataGrid图标列的定义

博客围绕DataGrid展开,探讨如何在某列同时显示图标和数据,且根据数据显示对应图标。提出重定义DataGridColumn的方法,定义了继承DataGridTextBoxColumn的DataGridImageColumn,还为使图标不固定定义了委托,最后给出了转载来源。

        如何在DataGrid中某一列既有图标又有数据,而且能够实现什么样的数据显示什么样的图标。对于这样的列,就只能是自己重定义一个DataGridColumn了(在2.0中有一个DataGridImageColumn,但是它好像还只能显示图标,对于数据没有办法显示,说真的2.0的变化有点大,很多东西还不会用emembarrassed.gif)。在这里也定义了一个 DataGridImageColumn,继承DataGridTextBoxColumn。主要功能是能够显示指定的图标,并且也能够显示对应的数据信息。
        为了显示的图标不在DataGridColumn中定死,故先定义了一个委托

1None.gifpublic delegate void FormatImageCellEventHandler(object sender, DataGridImageCellEventArgs e);
和 DataGridImageCellEventArgs
None.gifpublic class DataGridImageCellEventArgs:EventArgs
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
//
InBlock.gif
        private string _column;
InBlock.gif
//
InBlock.gif
        private int _row;
InBlock.gif
//需要显示的图片信息
InBlock.gif
        private Image _image;
InBlock.gif
InBlock.gif        
public DataGridImageCellEventArgs(string ColumnName,int RowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Column = ColumnName;
InBlock.gif            
this.Row = RowNum;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Image Image
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _image;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _image 
=  value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Column
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _column;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _column 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Row
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _row;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _row 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

注意这里的图片格式是 16×16

再实现DataGridImageColumn

None.gifpublic class DataGridImageBarColumn:System.Windows.Forms.DataGridTextBoxColumn
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private int _rowNum=-1;
InBlock.gif        
public DataGridImageBarColumn():base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置列图片信息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event FormatImageCellEventHandler SetImage;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif    
InBlock.gif            Rectangle rect 
= bounds;
InBlock.gif            
if(this.SetImage!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataGridImageCellEventArgs ei 
= new DataGridImageCellEventArgs(this.MappingName,rowNum);
InBlock.gif
//通过这个事件获取显示的图片信息
InBlock.gif
                this.SetImage(this,ei);
InBlock.gif
//                this.TextBox.Left = bounds.Location.X+(ei.Number+1)*16;
InBlock.gif
//                this.TextBox.Width = bounds.Width - (ei.Number+1)*16;
InBlock.gif
                rect =new Rectangle(bounds.Location.X+16,
InBlock.gif                    bounds.Location.Y,bounds.Width
-(ei.Number+1)*16,bounds.Height);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.Edit(source,rowNum, rect, readOnly, instantText , cellIsVisible);
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif            _rowNum 
= rowNum;
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataGridImageCellEventArgs ei 
= new DataGridImageCellEventArgs(this.MappingName,rowNum);
InBlock.gif            
if(this.SetImage!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SetImage(this,ei);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            DataRowView drv 
= source.List[rowNum] as DataRowView;
InBlock.gif            
if(drv == null)
InBlock.gif                
return;
InBlock.gif
InBlock.gif            Rectangle rect 
=bounds;
InBlock.gif            
if(!drv.Row.IsNull(this.MappingName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int leftX = 16;
InBlock.gif                g.FillRectangle(Brushes.White,bounds);
InBlock.gif                g.DrawImage(ei.Image,bounds.Location.X
+leftX,bounds.Location.Y);
InBlock.gif                rect 
= new Rectangle(bounds.X+16+leftX,bounds.Y+2,bounds.Width-17-leftX,bounds.Height);
ExpandedSubBlockEnd.gif            }

InBlock.gif                            
base.Paint(g, rect, source, rowNum, backBrush, foreBrush, alignToRight);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


这样在定义DataGridColumnStyle时可以定义这个列信息,犹如定义DataGridTextColumn,但是要比定义DataGridTextClumn多一个 SetImage事件的定义 ,这个事件是获取图片信息的。

        在下一篇中准备再次重写这个类,来实现DataGrid中 树的概念。和树一样可以有一种层叠的感觉。

转载于:https://www.cnblogs.com/xiaodele/archive/2005/06/01/166337.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值