DataGridImageColumnStyle 源码:using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Globalization; using System.Diagnostics;namespace DataGridExtensions { /// <summary> /// DataGridImageColumnStyle 的摘要说明。 /// </summary> public class DataGridImageColumnStyle : DataGridColumnStyle { // UI Constants private int xMargin = 2; private int yMargin = 1;
/// <summary> /// Creates an new DataGridImageColumnStyle /// </summary> public DataGridImageColumnStyle() { } /// <summary> /// Called when the user or the Grid aborts an edit /// For example hits the Escape key /// </summary> protected override void Abort(int rowNum) { } /// <summary> /// Called for the 'from' cell, when the current cell changes. /// This is called whether there are changes to commit or not. /// If the user has not edited the value we simply hide the numericUpDownbox. /// InEdit is used to track whether the cell has been edited or not. /// </summary> protected override bool Commit(CurrencyManager dataSource, int rowNum) { return true; } /// <summary> /// Edit is called when a cell column is made the active cell /// We don't put the Grid into edit mode here - we wait for /// the user to change something before going into edit mode /// because we don't want to dirty the data model unecessarily /// </summary> protected override void Edit( CurrencyManager source , int rowNum , Rectangle bounds , bool readOnly , string instantText , bool cellIsVisible ) { } /// <summary> /// Called to get the minimum height that rows this column /// can be /// </summary> protected override int GetMinimumHeight() { return yMargin; } /// <summary> /// Called to get the preferred height of the cell /// /// This called to calculate the preferred height for a row when a /// row is auto-resized by double-clicking on the row border in /// the row header /// /// Its also used to paint the parent row when drilled into a child, however /// in both cases only the width is used never the height /// </summary> protected override int GetPreferredHeight(Graphics g, object value) { return Math.Min(this.GetPreferredSize(g, value).Height, this.GetMinimumHeight()); } /// <summary> /// Called to get the preferred size of the cell /// /// This called to calculate the preferred width for a column when a /// column is auto-resized by double-clicking on the column border in /// the header /// /// Its also used to paint the parent row when drilled into a child, however /// in both cases only the width is used never the height /// </summary> protected override Size GetPreferredSize(Graphics g, object value) { int dataGridLineWidth = this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid ? 1 : 0 ; string path = value.ToString(); Image i = Image.FromFile(path); Size preferredSize = new Size(0, 0); preferredSize.Width = xMargin * 2 + dataGridLineWidth + i.Width; preferredSize.Height = yMargin + i.Height; return preferredSize; } protected override void Paint( Graphics g , Rectangle bounds , CurrencyManager currencyManager , int rowNum , Brush backBrush , Brush foreBrush , bool alignToRight) { string path = GetColumnValueAtRow(currencyManager,rowNum).ToString(); g.DrawImage(Image.FromFile(path), bounds, 0, 0, bounds.Width, bounds.Height, GraphicsUnit.Pixel); }
/// <summary> /// Paint 2: Called to paint the contents of a particular cell. /// This simply calls Paint 3. /// </summary> protected override void Paint( Graphics g , Rectangle bounds , CurrencyManager currencyManager , int rowNum) { Paint(g, bounds, currencyManager, rowNum, false); } /// <summary> /// Paint 3: Called to paint the contents of a particular cell. /// This simply calls Paint 1. /// </summary> protected override void Paint( Graphics g , Rectangle bounds , CurrencyManager currencyManager , int rowNum , bool alignToRight) { using (Brush backBrush = new SolidBrush(this.DataGridTableStyle.BackColor)) { using (Brush foreBrush = new SolidBrush(this.DataGridTableStyle.ForeColor)) { Paint(g, bounds, currencyManager, rowNum, backBrush, foreBrush, alignToRight); } } } } }
调用示例:DataGridTableStyle tableStyle = new DataGridTableStyle();DataGridNumericUpDownColumnStyle numCol = new DataGridNumericUpDownColumnStyle(); numCol.MappingName = "ValueColumn"; numCol.HeaderText = "ValueColumn"; numCol.Width = 100;tableStyle.GridColumnStyles.Add(numCol);this.dataGrid1.TableStyles.Add(tableStyle);
楼主的讲的是有的道理,能有些具体的代码吗,谢谢。
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;namespace DataGridExtensions
{
/// <summary>
/// DataGridImageColumnStyle 的摘要说明。
/// </summary>
public class DataGridImageColumnStyle : DataGridColumnStyle
{
// UI Constants
private int xMargin = 2;
private int yMargin = 1;
/// <summary>
/// Creates an new DataGridImageColumnStyle
/// </summary>
public DataGridImageColumnStyle()
{
} /// <summary>
/// Called when the user or the Grid aborts an edit
/// For example hits the Escape key
/// </summary>
protected override void Abort(int rowNum)
{
} /// <summary>
/// Called for the 'from' cell, when the current cell changes.
/// This is called whether there are changes to commit or not.
/// If the user has not edited the value we simply hide the numericUpDownbox.
/// InEdit is used to track whether the cell has been edited or not.
/// </summary>
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
return true;
} /// <summary>
/// Edit is called when a cell column is made the active cell
/// We don't put the Grid into edit mode here - we wait for
/// the user to change something before going into edit mode
/// because we don't want to dirty the data model unecessarily
/// </summary>
protected override void Edit( CurrencyManager source
, int rowNum
, Rectangle bounds
, bool readOnly
, string instantText
, bool cellIsVisible
)
{
} /// <summary>
/// Called to get the minimum height that rows this column
/// can be
/// </summary>
protected override int GetMinimumHeight()
{
return yMargin;
} /// <summary>
/// Called to get the preferred height of the cell
///
/// This called to calculate the preferred height for a row when a
/// row is auto-resized by double-clicking on the row border in
/// the row header
///
/// Its also used to paint the parent row when drilled into a child, however
/// in both cases only the width is used never the height
/// </summary>
protected override int GetPreferredHeight(Graphics g, object value)
{
return Math.Min(this.GetPreferredSize(g, value).Height, this.GetMinimumHeight());
} /// <summary>
/// Called to get the preferred size of the cell
///
/// This called to calculate the preferred width for a column when a
/// column is auto-resized by double-clicking on the column border in
/// the header
///
/// Its also used to paint the parent row when drilled into a child, however
/// in both cases only the width is used never the height
/// </summary>
protected override Size GetPreferredSize(Graphics g, object value)
{
int dataGridLineWidth = this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid ? 1 : 0 ;
string path = value.ToString();
Image i = Image.FromFile(path); Size preferredSize = new Size(0, 0); preferredSize.Width = xMargin * 2 + dataGridLineWidth + i.Width;
preferredSize.Height = yMargin + i.Height; return preferredSize;
} protected override void Paint( Graphics g
, Rectangle bounds
, CurrencyManager currencyManager
, int rowNum
, Brush backBrush
, Brush foreBrush
, bool alignToRight)
{
string path = GetColumnValueAtRow(currencyManager,rowNum).ToString();
g.DrawImage(Image.FromFile(path), bounds, 0, 0, bounds.Width, bounds.Height, GraphicsUnit.Pixel);
}
/// <summary>
/// Paint 2: Called to paint the contents of a particular cell.
/// This simply calls Paint 3.
/// </summary>
protected override void Paint( Graphics g
, Rectangle bounds
, CurrencyManager currencyManager
, int rowNum)
{
Paint(g, bounds, currencyManager, rowNum, false);
} /// <summary>
/// Paint 3: Called to paint the contents of a particular cell.
/// This simply calls Paint 1.
/// </summary>
protected override void Paint( Graphics g
, Rectangle bounds
, CurrencyManager currencyManager
, int rowNum
, bool alignToRight)
{
using (Brush backBrush = new SolidBrush(this.DataGridTableStyle.BackColor))
{
using (Brush foreBrush = new SolidBrush(this.DataGridTableStyle.ForeColor))
{
Paint(g, bounds, currencyManager, rowNum, backBrush, foreBrush, alignToRight);
}
}
}
}
}
numCol.MappingName = "ValueColumn";
numCol.HeaderText = "ValueColumn";
numCol.Width = 100;tableStyle.GridColumnStyles.Add(numCol);this.dataGrid1.TableStyles.Add(tableStyle);