重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;
}
转载于:https://www.cnblogs.com/timsoft/articles/414991.html