在DataGrid中添加图片列(C#)

重DataGridTextBoxColumn类
None.gif using  System;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Drawing;
None.gif
using  System.Data;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  ImageCellInDataGrid
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for DataGridImageCell.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataGridImageCell : DataGridTextBoxColumn
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private ArrayList theImages1;
InBlock.gif        
public DataGridImageCell()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public ArrayList theImages
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return theImages1;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{theImages1 = value;}
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            
object o = this.GetColumnValueAtRow(source, rowNum);
InBlock.gif            
if(o != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i = (int)o;
InBlock.gif                g.FillRectangle(backBrush, bounds);
InBlock.gif                
InBlock.gif                Bitmap bmp 
= (Bitmap)theImages[i];
InBlock.gif            
InBlock.gif                
//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.NoResize;
InBlock.gif                
//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitProportionally;
InBlock.gif
                GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitToCell;
InBlock.gif                
InBlock.gif                
InBlock.gif                System.Drawing.GraphicsUnit gu 
= System.Drawing.GraphicsUnit.Point;
InBlock.gif                    
InBlock.gif                RectangleF srcRect 
= bmp.GetBounds(ref gu);
InBlock.gif                Rectangle destRect 
= Rectangle.Empty;
InBlock.gif
InBlock.gif                Region saveRegion 
= g.Clip;
InBlock.gif
InBlock.gif                
switch(cellDrawOption)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case GridImageCellDrawOption.FitToCell:
InBlock.gif                        destRect 
= bounds;
InBlock.gif                        
break;
InBlock.gif                    
case GridImageCellDrawOption.NoResize:
InBlock.gif                        destRect 
= new Rectangle(bounds.X, bounds.Y, (int) srcRect.Width, (int) srcRect.Height);
InBlock.gif                        g.Clip 
= new Region(bounds);
InBlock.gif                        
break;
InBlock.gif                    
case GridImageCellDrawOption.FitProportionally:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
float srcRatio =  srcRect.Width / srcRect.Height;
InBlock.gif                        
float tarRatio = (float) bounds.Width / bounds.Height;
InBlock.gif                        destRect 
= bounds;
InBlock.gif                        
if( tarRatio < srcRatio )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            destRect.Height 
= (int) (destRect.Width * srcRatio);    
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            destRect.Width 
= (int) (destRect.Height * srcRatio);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                        
break;
InBlock.gif                        
InBlock.gif                    
default:
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if(!destRect.IsEmpty)
InBlock.gif                    g.DrawImage(bmp,  destRect, srcRect, gu);
InBlock.gif
InBlock.gif                g.Clip 
= saveRegion;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.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        
//overridden to avoid editing
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public enum GridImageCellDrawOption
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FitToCell      
= 0,
InBlock.gif            NoResize      
= 1,
InBlock.gif            FitProportionally 
= 2,
ExpandedSubBlockEnd.gif        }
;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
测试代码
None.gif private  System.Windows.Forms.DataGrid dataGrid1;
None.gif
None.gif
private   void  Form1_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
//create a datatable
InBlock.gif
            DataTable dt = new DataTable("MyTable");
InBlock.gif
InBlock.gif            dt.Columns.Add(
new DataColumn("Col0"));
InBlock.gif            dt.Columns.Add(
new DataColumn("Images",typeof(int)));
InBlock.gif
InBlock.gif            Random r 
= new Random();
InBlock.gif            
for(int i = 0; i < nRows; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow dr 
= dt.NewRow();
InBlock.gif                dr[
0= string.Format("row{0}", i);
InBlock.gif                dr[
1= r.Next(4);
InBlock.gif                dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//store bitmaps in an arraylist
InBlock.gif
            bitMaps = new ArrayList();
InBlock.gif            System.IO.Stream strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Blue hills.jpg");
InBlock.gif            bitMaps.Add(
new Bitmap(strm));
InBlock.gif            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Sunset.jpg");
InBlock.gif            bitMaps.Add(
new Bitmap(strm));
InBlock.gif            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Water lilies.jpg");
InBlock.gif            bitMaps.Add(
new Bitmap(strm));
InBlock.gif            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Winter.jpg");
InBlock.gif            bitMaps.Add(
new Bitmap(strm));
InBlock.gif            
InBlock.gif
InBlock.gif            DataGridTableStyle tableStyle 
= new DataGridTableStyle();
InBlock.gif            tableStyle.MappingName 
= "MyTable";
InBlock.gif
InBlock.gif            DataGridTextBoxColumn tbc 
= new DataGridTextBoxColumn();
InBlock.gif            tbc.MappingName 
= "Col0";
InBlock.gif            tbc.HeaderText 
= "Column 1";
InBlock.gif            tbc.Width 
= 50;
InBlock.gif            tableStyle.GridColumnStyles.Add(tbc);
InBlock.gif
InBlock.gif            
int width = this.dataGrid1.ClientSize.Width - tbc.Width - this.dataGrid1.RowHeaderWidth - 4;
InBlock.gif            DataGridImageCell tbc1 
= new DataGridImageCell();
InBlock.gif            tbc1.MappingName 
= "Images";
InBlock.gif            tbc1.HeaderText 
= "Images";
InBlock.gif            tbc1.theImages 
= bitMaps;
InBlock.gif            tbc1.Width 
= width;
InBlock.gif            tableStyle.GridColumnStyles.Add(tbc1);
InBlock.gif
InBlock.gif            
this.dataGrid1.TableStyles.Add(tableStyle);
InBlock.gif            
this.dataGrid1.DataSource = dt;
InBlock.gif            dt.DefaultView.AllowNew 
= false;
InBlock.gif
InBlock.gif            Rectangle rect 
= this.dataGrid1.GetCellBounds(0,0);
InBlock.gif            topPos 
= rect.Top;
InBlock.gif            
int height = (this.dataGrid1.ClientSize.Height - topPos - nRows) / nRows;
InBlock.gif            tableStyle.PreferredRowHeight 
= height;
InBlock.gif            
this.dataGrid1.DataSource = null;
InBlock.gif            
this.dataGrid1.DataSource = dt;
InBlock.gif            
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/timsoft/articles/414991.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值