问题描述
你好,
首先,对不起我的英语。我有一个包含多个列的DataGridView,在其中一个列中,我想显示文本和图像,但位于不同的位置。我发现源代码在文本之前表示图像,或在图像之前表示文本,但不是图像之间的文本,或文本之间的图像,这就是我想要做的事情。
谢谢很多。
从评论中移出:
我使用TextAndImageCell类来显示文本和图像,但这个类只让我展示一个文字前的图片...
这是我用来调用类TextAndImageCell的源代码
图像image = Image.FromFile(imageFilePath);
((TextAndImageCell)dgvChat.Rows [e.RowIndex] .Cells [mensaje])。Image = image;
这是TextAndImageCell方法的源代码绘制图像和文本
; protected 覆盖 void Paint(图形图形,矩形clipBounds,
矩形cellBounds, int rowIndex,DataGridViewElementStates cellState,
object value , object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// 绘制基本内容
碱跨度> .Paint(图形,clipBounds,cellBounds,rowIndex,cellState,
value ,formattedValue,errorText,cellStyle,
advancedBorderStyle,paintParts);
if ( this .Image!= null )
{
// 绘制剪裁到的图像单元格。
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
Point newLocation = new Point(cellBounds.Location.X,cellBounds.Location.Y + 2 );
// graphics.DrawImageUnscaled(this.Image,cellBounds.Location);
graphics.DrawImageUnscaled( this .Image,newLocation);
graphics.EndContainer(container);
}
}
Hello,
First of all, sorry for my english. I have a DataGridView with several columns, and, in one of those, I want to show text and image, but in different positions. I have found sourcecode to represent image before text, or text before image, but not text between images, or image between text, that's what I want to do.
Thanks a lot.
Moved from comments:
I use a TextAndImageCell class to show text and image, but this class only let me show an image before the text...
This is the sourcecode that I use to call the class TextAndImageCell
Image image = Image.FromFile(imageFilePath); ((TextAndImageCell)dgvChat.Rows[e.RowIndex].Cells[mensaje]).Image = image;
And this is the sourcecode of TextAndImageCell method paint the image and text
;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); Point newLocation = new Point(cellBounds.Location.X, cellBounds.Location.Y + 2); //graphics.DrawImageUnscaled(this.Image, cellBounds.Location); graphics.DrawImageUnscaled(this.Image, newLocation); graphics.EndContainer(container); } }
这篇关于DataGridViewCell中的图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!