扩展 DataGridView 控件中单元格和列
实现功能:在自定义列的单元格中,显示 图像+文字功能。即在一个cell中显示图像+文字
DataGridView实现了6种列和Cell。分别如下
==列=
System.Windows.Forms.DataGridViewButtonColumn
System.Windows.Forms.DataGridViewCheckBoxColumn
System.Windows.Forms.DataGridViewComboBoxColumn
System.Windows.Forms.DataGridViewImageColumn
System.Windows.Forms.DataGridViewLinkColumn
System.Windows.Forms.DataGridViewTextBoxColumn
Cell
System.Windows.Forms.DataGridViewButtonCell
System.Windows.Forms.DataGridViewCheckBoxCell
System.Windows.Forms.DataGridViewComboBoxCell
System.Windows.Forms.DataGridViewHeaderCell
System.Windows.Forms.DataGridViewImageCell
System.Windows.Forms.DataGridViewLinkCell
System.Windows.Forms.DataGridViewTextBoxCell
备注:
当从 DataGridViewCell 或 DataGridViewColumn 进行派生并将新属性添加到派生的类时,请确保重写 Clone 方法以在克隆操作过程中复制新属性。 还应调用基类的 Clone 方法,以便将基类的属性复制到新的单元格或列
单元格中要显示的数据类
public class TextAndImgData
{
public string CellText { get; set; }
public Image CellImage { get; set; }
}
扩展DataGridViewTextBoxCell
public class MyTextAndImageCell : DataGridViewTextBoxCell
{
private Size imgSize;
public MyTextAndImageCell() : base()
{
imgSize = new Size(21, 21);
}
private TextAndImgData _cellData;
public TextAndImgData CellData
{
get { return _cellData; }
set
{
_cellData = value;
//Padding inheritedPadding = this.InheritedStyle.Padding;
//让文字与cell左边相距 imgSize.Width + 10 个像素
this.Style.Padding = new Padding(imgSize.Width + 10, 0, 0, 0);
}
}
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
if (value is TextAndImgData)
{
TextAndImgData cellData = value as TextAndImgData;
this.CellData = cellData;
}
else
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
if (this.CellData != null)
{
formattedValue = CellData.CellText;
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, CellData.CellText, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
//绘制图片
if (this.CellData.CellImage != null)
{
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
Rectangle destImgRec = new Rectangle();
destImgRec.X = cellBounds.Left + 5;
destImgRec.Y = cellBounds.Y + (cellBounds.Height - imgSize.Height) / 2;
destImgRec.Width = imgSize.Width;
destImgRec.Height = imgSize.Height;
graphics.DrawImage(this.CellData.CellImage, destImgRec);
//graphics.DrawImageUnscaled(this.CellData.CellImage, cellBounds.Location);
graphics.EndContainer(container);
}
}
}
public override object Clone()
{
MyTextAndImageCell c = base.Clone() as MyTextAndImageCell;
c.CellData = this.CellData;
return c;
}
}
扩展DataGridViewColumn
public class MyTextAndImageColumn : DataGridViewColumn
{
public MyTextAndImageColumn()
{
//通过CellTemplate来设置该列要显示的单元格内容
this.CellTemplate = new MyTextAndImageCell();
}
public override object Clone()
{
MyTextAndImageColumn c = base.Clone() as MyTextAndImageColumn;
return c;
}
}
虚拟模式下使用方式
this.dataGridView1.VirtualMode = true;
this.dataGridView1.ColumnCount = 1;
this.this.dataGridView1.RowCount = FileDataSource.Files.Count;
//虚拟模式下处理CellValueNeeded事件来显示数据
this.dataGridView1.CellValueNeeded += DataGridView1_CellValueNeeded;
private void DataGridView1_CellValueNeeded(object? sender, DataGridViewCellValueEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex == -1) return;
if (rowIndex < FileDataSource.QueryResultFiles.Count)
{
string colName = dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
switch (colIndex)
{
case 0:
{
TextAndImgData imgData = new TextAndImgData();
imgData.CellText = EnrichFileModelMethod(FileDataSource.QueryResultFiles[e.RowIndex]).FileName;
imgData.CellImage = ImgHelper.GetIcon(EnrichFileModelMethod(FileDataSource.QueryResultFiles[e.RowIndex]).FilePath).ToBitmap();
e.Value = imgData;
break;
}
}
//dataGridView1.Rows.Add();这一行不需要
}
// 从记录集中读取数据
}
非虚拟模式-数据源绑定方式使用
public class MyFileInfo
{
public TextAndImgData FileName { get; set; }
}
this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView1.RowTemplate.Height = 55;
//this.dataGridView1.ColumnHeadersHeight = 30;
MyTextAndImageColumn column = new MyTextAndImageColumn();
column.Name = "FileName";
column.HeaderText = "Name";
column.DataPropertyName = "FileName"; //设置数据源名称
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
column.Width = 300;
// column.DefaultCellStyle.Padding = new System.Windows.Forms.Padding(32,0,0,0);
this.dataGridView1.Columns.Add(column);
this.dataGridView1.AutoGenerateColumns = false;
List<MyFileInfo> data = new List<MyFileInfo>();
MyFileInfo f1 = new MyFileInfo();
TextAndImgData cd1 = new TextAndImgData();
cd1.CellText = @"E:\tools\MicrosoftVisualStudio10.0\Samples\2052";
cd1.CellImage = ImgHelper.GetIcon(cd1.CellText).ToBitmap();
f1.FileName = cd1;
data.Add(f1);
MyFileInfo f2 = new MyFileInfo();
TextAndImgData cd2 = new TextAndImgData();
cd2.CellText = @"E:\tools\MicrosoftVisualStudio10.0\Samples\2052\CSharpSamples.zip";
cd2.CellImage = ImgHelper.GetIcon(cd2.CellText).ToBitmap();
f2.FileName = cd2;
data.Add(f2);
this.dataGridView1.DataSource = data;