class ImageTextCellValue
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
private Image img;
public Image Image
{
get { return img; }
set { img = value; }
}
}
class DataGridViewImageTextBoxCell : DataGridViewImageCell
{
public DataGridViewImageTextBoxCell()
{
}
public override object Clone()
{
DataGridViewImageTextBoxCell cell = (DataGridViewImageTextBoxCell)base.Clone();
return cell;
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value is Image)
return (value as Image).ToString();
else
return (value as ImageTextCellValue).Text;
//return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
if (this.Selected)
cellBackground = new SolidBrush(cellStyle.SelectionBackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
Image img = null;
string text = "";
if (value is Image)
{
img = (Image)value;
}
else if (value is ImageTextCellValue)
{
img = (value as ImageTextCellValue).Image;
text = (value as ImageTextCellValue).Text;
}
if (img != null)
{
Rectangle imageRect = new Rectangle(cellBounds.X + 4, cellBounds.Y, 16, 16);
graphics.DrawImage(img, imageRect);
}
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
int txtWidth = (int)graphics.MeasureString(text, cellStyle.Font).Width + 2;
SolidBrush textBrush = new SolidBrush(cellStyle.ForeColor);
if (this.Selected)
textBrush = new SolidBrush(cellStyle.SelectionForeColor);
graphics.DrawString(text, cellStyle.Font, textBrush, new Rectangle(cellBounds.X + 20, cellBounds.Y, txtWidth, cellBounds.Height), sf);
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
class DataGridViewImageTextBoxColumn : DataGridViewImageColumn
{
public DataGridViewImageTextBoxColumn()
{
this.CellTemplate = new DataGridViewImageTextBoxCell();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewImageTextBoxCell)))
{
throw new Exception("这个列里面必须邦定DataGridViewImageTextBoxCell");
}
base.CellTemplate = value;
}
}
}