DataGridView一个格子中显示图片和文字,请高手帮忙。

SE_ZheHao 2009-12-04 09:16:58
C#,怎样在Datagridview的一列中显示左边为一个图标文件 右边为text?请详细回答,比告诉我用模板列就可以了。。网上找到类似代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace JrtGenerator.Utility
{
public class TextAndImageColumn : DataGridViewTextBoxColumn
{
private Image imageValue;
private Size imageSize;
public TextAndImageColumn()
{
this.CellTemplate = new TextAndImageCell();
}

public override object Clone()
{
TextAndImageColumn c = base.Clone() as TextAndImageColumn;
c.imageValue = this.imageValue;
c.imageSize = this.imageSize;
return c;
}

public Image Image
{
get
{
return this.imageValue;
}
set
{
if (this.Image != value)
{
this.imageValue = value;
this.imageSize = value.Size;
if (this.InheritedStyle != null)
{
Padding inheritedPadding = this.InheritedStyle.Padding;
this.DefaultCellStyle.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}
}

private TextAndImageCell TextAndImageCellTemplate
{
get
{
return this.CellTemplate as TextAndImageCell;
}
}
internal Size ImageSize
{
get
{
return imageSize;
}
}
}

public class TextAndImageCell : DataGridViewTextBoxCell
{
private Image imageValue;

private Size imageSize;

public override object Clone()
{
TextAndImageCell c = base.Clone() as TextAndImageCell;
c.imageValue = this.imageValue;
c.imageSize = this.imageSize;
return c;
}

public Image Image
{
get
{
if (this.OwningColumn == null || this.OwningTextAndImageColumn == null)
{
return imageValue;
}
else if (this.imageValue != null)
{
return this.imageValue;
}
else
{
return this.OwningTextAndImageColumn.Image;
}
}
set
{
if (this.imageValue != value)
{
this.imageValue = value;
this.imageSize = value.Size;
Padding inheritedPadding = this.InheritedStyle.Padding;
this.Style.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}

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
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (this.Image != null)
{
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(this.Image, cellBounds.Location);
graphics.EndContainer(container);
}
}

private TextAndImageColumn OwningTextAndImageColumn
{
get
{
return this.OwningColumn as TextAndImageColumn;
}
}
}

}
可是,它只实现了,在这列中添加一个Image属性,而我想在这一列的每一个格子中显示不同的图片,就是说最好可以像
this.datagridview.rows.add(??) ??处就可以包含图片的路径 和 文字的string ?
...全文
448 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
SE_ZheHao 2009-12-04
  • 打赏
  • 举报
回复
恩,答是有点非所问,不过给的链接很不错哦~
wuyq11 2009-12-04
  • 打赏
  • 举报
回复
datagridview里显示图片。
DataGridViewImageColumn _ImageColumn = new DataGridViewImageColumn();
dataGridView1.Columns.Add(_ImageColumn);
dataGridView1.Rows.Add(new object[] { this.Icon.ToBitmap()});


private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("Image"))
{
string path = e.Value.ToString();
e.Value = GetImage(path);
}
}
public System.Drawing.Image GetImage(string path)
{
return System.Drawing.Image.FromFile(path);
}
单元格显示图片也显示文字
kafanqd 2009-12-04
  • 打赏
  • 举报
回复
帮你顶!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧