在网上下载个类public class DataGridImageColumnStyle,直接引用的。引用后datagrid里面没显示出相应图片,请帮忙
public class DataGridImageColumnStyle:DataGridColumnStyle
{
private int xMargin = 2;
private int yMargin = 1;
/// <summary>
/// 构造方法,建立一个图型格式的列
/// </summary>
public DataGridImageColumnStyle()
{
}
/// <summary>
/// 当用户或表格中断时调用,如击键"esc"
/// </summary>
protected override void Abort(int rowNum)
{
}
/// <summary>
/// 当前表格内容改变时,调用'from' 表格
/// 不管改变是否提交,此方法将被调用
/// 如果用户没有编辑值,隐藏按下键盘
/// InEdit 用于跟踪是否发生改变.
/// </summary>
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
return true;
}
/// <summary>
/// 当表格列被激活时编辑
/// 不将表格置于编辑模式,在进入编辑模式前等待,因为不想脏读数据
/// </summary>
protected override void Edit( CurrencyManager source
, int rowNum
, Rectangle bounds
, bool readOnly
, string instantText
, bool cellIsVisible
)
{
}
/// <summary>
/// 取得此列的最小行高
/// </summary>
protected override int GetMinimumHeight()
{
return yMargin;
}
/// <summary>
/// 取得表格的高度
/// </summary>
protected override int GetPreferredHeight(Graphics g, object value)
{
return Math.Min(this.GetPreferredSize(g, value).Height, this.GetMinimumHeight());
}
/// <summary>
/// 取得表格尺寸
/// </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.
/// </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.
/// </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);
}
}
}
}
以上是类的代码,下面是引用代码。
DataTable dt = new DataTable("dt");
// dt.Columns.Add(new DataColumn("imagecol"));
DataColumn dateColumn = new DataColumn
("imagecol", typeof(DataGridImageColumnStyle));
dt.Columns.Add(dateColumn);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dg.DataSource = ds;
DataGridTableStyle tableStyle = new DataGridTableStyle(); //tbale的样式
tableStyle.MappingName="dt";//设置datatable名 DataGridImageColumnStyle numCol = new DataGridImageColumnStyle(); //column样式
numCol.MappingName = "imagecol";
numCol.HeaderText = "imagecol";
numCol.Width = 100; tableStyle.GridColumnStyles.Add(numCol);//将col样式加入table样式中 DataRow dRow = dt.NewRow();
PictureBox pb=new PictureBox();
pb.Image=Image.FromFile(@"C:\2.bmp");
dRow["imagecol"] =pb; dt.Rows.Add(dRow);
dt.AcceptChanges();
请各位帮忙
public class DataGridImageColumnStyle:DataGridColumnStyle
{
private int xMargin = 2;
private int yMargin = 1;
/// <summary>
/// 构造方法,建立一个图型格式的列
/// </summary>
public DataGridImageColumnStyle()
{
}
/// <summary>
/// 当用户或表格中断时调用,如击键"esc"
/// </summary>
protected override void Abort(int rowNum)
{
}
/// <summary>
/// 当前表格内容改变时,调用'from' 表格
/// 不管改变是否提交,此方法将被调用
/// 如果用户没有编辑值,隐藏按下键盘
/// InEdit 用于跟踪是否发生改变.
/// </summary>
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
return true;
}
/// <summary>
/// 当表格列被激活时编辑
/// 不将表格置于编辑模式,在进入编辑模式前等待,因为不想脏读数据
/// </summary>
protected override void Edit( CurrencyManager source
, int rowNum
, Rectangle bounds
, bool readOnly
, string instantText
, bool cellIsVisible
)
{
}
/// <summary>
/// 取得此列的最小行高
/// </summary>
protected override int GetMinimumHeight()
{
return yMargin;
}
/// <summary>
/// 取得表格的高度
/// </summary>
protected override int GetPreferredHeight(Graphics g, object value)
{
return Math.Min(this.GetPreferredSize(g, value).Height, this.GetMinimumHeight());
}
/// <summary>
/// 取得表格尺寸
/// </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.
/// </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.
/// </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);
}
}
}
}
以上是类的代码,下面是引用代码。
DataTable dt = new DataTable("dt");
// dt.Columns.Add(new DataColumn("imagecol"));
DataColumn dateColumn = new DataColumn
("imagecol", typeof(DataGridImageColumnStyle));
dt.Columns.Add(dateColumn);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dg.DataSource = ds;
DataGridTableStyle tableStyle = new DataGridTableStyle(); //tbale的样式
tableStyle.MappingName="dt";//设置datatable名 DataGridImageColumnStyle numCol = new DataGridImageColumnStyle(); //column样式
numCol.MappingName = "imagecol";
numCol.HeaderText = "imagecol";
numCol.Width = 100; tableStyle.GridColumnStyles.Add(numCol);//将col样式加入table样式中 DataRow dRow = dt.NewRow();
PictureBox pb=new PictureBox();
pb.Image=Image.FromFile(@"C:\2.bmp");
dRow["imagecol"] =pb; dt.Rows.Add(dRow);
dt.AcceptChanges();
请各位帮忙
解决方案 »
- 高分求 怎么取到 我的电脑 的图标
- H.264 .net 开发包
- 郁闷
- 如何实现一个BUTTON按扭 点击 一下,就改变上面的字啊
- 请问 HTMLDocumentClass的execCommand执行的方法列表
- 经常计算的字段的设计
- VS.NET2003中的属性或解决方案资源管理器等一系列窗口,按小按钮后可以固定位置,或当鼠标over时才伸展出来,没有over时就自动缩加隐藏,
- 500分求Infragistics NetAdvantage 2003 Vol 3 .net 版,顶贴有分
- C# 有个Web 客户端打印的类, 一下找不到了.
- 这个小程序有有点问题!
- this关键字
- 如何提取一个图片中的图形?图形是单一色的!谢谢!
pb.Image=Image.FromFile(@"C:\2.bmp");
dRow["imagecol"] =pb;//这里好像不能设置成PictureBox,而是一个图片的路径。因为在
DataGridImageColumnStyle的Paint方法是通过图片路径来画图的。
------------------------
不是楼主早以知道这里是取的图片的路径吗?只要你的相应的DataTable中的列中存放的是你本地机器上的图片的路径应该可以显示出来了。