当前位置:首页 > 博客园 >在DataGrid中添加图片列C#

在DataGrid中添加图片列C#

  作者:Tim工作室   来源: 博客园-timsoft  
重DataGridTextBoxColumn类
using  System;
using  System.Windows.Forms;
using  System.Drawing;
using  System.Data;
using  System.Collections;

namespace  ImageCellInDataGrid
{
    
/**//// <summary>
    
/// Summary description for DataGridImageCell.
    
/// </summary>

    public class DataGridImageCell : DataGridTextBoxColumn
    
{
        
private ArrayList theImages1;
        
public DataGridImageCell()
        
{
            
        }

        
public ArrayList theImages
        
{
            
get{return theImages1;}
            
set{theImages1 = value;}
        }


        
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)
        
{
            
object o = this.GetColumnValueAtRow(source, rowNum);
            
if(o != null)
            
{
                
int i = (int)o;
                g.FillRectangle(backBrush, bounds);
                
                Bitmap bmp 
= (Bitmap)theImages[i];
            
                
//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.NoResize;
                
//GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitProportionally;
                GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitToCell;
                
                
                System.Drawing.GraphicsUnit gu 
= System.Drawing.GraphicsUnit.Point;
                    
                RectangleF srcRect 
= bmp.GetBounds(ref gu);
                Rectangle destRect 
= Rectangle.Empty;

                Region saveRegion 
= g.Clip;

                
switch(cellDrawOption)
                
{
                    
case GridImageCellDrawOption.FitToCell:
                        destRect 
= bounds;
                        
break;
                    
case GridImageCellDrawOption.NoResize:
                        destRect 
= new Rectangle(bounds.X, bounds.Y, (int) srcRect.Width, (int) srcRect.Height);
                        g.Clip 
= new Region(bounds);
                        
break;
                    
case GridImageCellDrawOption.FitProportionally:
                    
{
                        
float srcRatio =  srcRect.Width / srcRect.Height;
                        
float tarRatio = (float) bounds.Width / bounds.Height;
                        destRect 
= bounds;
                        
if( tarRatio < srcRatio )
                        
{
                            destRect.Height 
= (int) (destRect.Width * srcRatio);    
                        }

                        
else
                        
{
                            destRect.Width 
= (int) (destRect.Height * srcRatio);
                        }

                    }

                        
break;
                        
                    
default:
                        
break;
                }


                
if(!destRect.IsEmpty)
                    g.DrawImage(bmp,  destRect, srcRect, gu);

                g.Clip 
= saveRegion;
            }

        }


        
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        
{
        
//overridden to avoid editing
        }


        
public enum GridImageCellDrawOption
        
{
            FitToCell      
= 0,
            NoResize      
= 1,
            FitProportionally 
= 2,
        }
;
    }

}

测试代码
private  System.Windows.Forms.DataGrid dataGrid1;

private   void  Form1_Load( object  sender, System.EventArgs e)
        
{

            
//create a datatable
            DataTable dt = new DataTable("MyTable");

            dt.Columns.Add(
new DataColumn("Col0"));
            dt.Columns.Add(
new DataColumn("Images",typeof(int)));

            Random r 
= new Random();
            
for(int i = 0; i < nRows; ++i)
            
{
                DataRow dr 
= dt.NewRow();
                dr[
0= string.Format("row{0}", i);
                dr[
1= r.Next(4);
                dt.Rows.Add(dr);
            }


            
//store bitmaps in an arraylist
            bitMaps = new ArrayList();
            System.IO.Stream strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Blue hills.jpg");
            bitMaps.Add(
new Bitmap(strm));
            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Sunset.jpg");
            bitMaps.Add(
new Bitmap(strm));
            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Water lilies.jpg");
            bitMaps.Add(
new Bitmap(strm));
            strm 
= GetType().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Winter.jpg");
            bitMaps.Add(
new Bitmap(strm));
            

            DataGridTableStyle tableStyle 
= new DataGridTableStyle();
            tableStyle.MappingName 
= "MyTable";

            DataGridTextBoxColumn tbc 
= new DataGridTextBoxColumn();
            tbc.MappingName 
= "Col0";
            tbc.HeaderText 
= "Column 1";
            tbc.Width 
= 50;
            tableStyle.GridColumnStyles.Add(tbc);

            
int width = this.dataGrid1.ClientSize.Width - tbc.Width - this.dataGrid1.RowHeaderWidth - 4;
            DataGridImageCell tbc1 
= new DataGridImageCell();
            tbc1.MappingName 
= "Images";
            tbc1.HeaderText 
= "Images";
            tbc1.theImages 
= bitMaps;
            tbc1.Width 
= width;
            tableStyle.GridColumnStyles.Add(tbc1);

            
this.dataGrid1.TableStyles.Add(tableStyle);
            
this.dataGrid1.DataSource = dt;
            dt.DefaultView.AllowNew 
= false;

            Rectangle rect 
= this.dataGrid1.GetCellBounds(0,0);
            topPos 
= rect.Top;
            
int height = (this.dataGrid1.ClientSize.Height - topPos - nRows) / nRows;
            tableStyle.PreferredRowHeight 
= height;
            
this.dataGrid1.DataSource = null;
            
this.dataGrid1.DataSource = dt;
            
        }

相关阅读:

来源:(微信/QQ:420434200,微信公众号:makaidong-com)
       欢迎分享本文,转载请保留出处!

       【原文阅读】:http://www.cnblogs.com/timsoft/articles/414991.html
  • 上一篇:没有了
  • 下一篇:谚语
没有了

  • 评论:
  • 赞助本站

发表一针见血的评论,你还等什么? ◎欢迎参与讨论,我的微信公众号是:makaidong-com
马开东博客赞助