Hi Aland,
Here is the code for TextAndImageColumn Class
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; }
}
}
And this is how I am displaying image with Text in the TextAndImageColumn.
Here in the TextAndImageColumn, according to column data value I want to display different image.
private void dgv_users_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex == 3)
{
if (e.FormattedValue.ToString().ToLower() == "in site")
{
((TextAndImageColumn)this.dgv_users.Columns[3]).Image = imglist.Images["offline.jpg"];
}
else if (e.FormattedValue.ToString().ToLower() == "waiting")
{
((TextAndImageColumn)this.dgv_users.Columns[3]).Image = imglist.Images["waiting.gif"];
}
}
}
This is working fine with me. For offline column value it's showing offline.jpg.
But for waiting column value I want to show waiting.gif , which is animated image and the frames are of same time.
So I took a reference of one of the reply given to you --
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0d9e790e-6816-40e7-96fe-bbf333a4abc0/
Now,By referring the above url I want to show Animated Image in the DatagridView column.
So I tried
private void Chat_Load(object sender, EventArgs e)
{
this.dgv_users.Paint += new
PaintEventHandler(dgv_users_Paint);
}
private void dgv_users_CellPainting(object
sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex == 3)
{
if (e.FormattedValue.ToString().ToLower() == "in site")
{
((TextAndImageColumn)this.dgv_users.Columns[3]).Image = imglist.Images["offline.jpg"];
}
else if (e.FormattedValue.ToString().ToLower() == "waiting")
{
((TextAndImageColumn)this.dgv_users.Columns[3]).Image = imglist.Images["waiting.gif"];
}
}
}
public void AnimateImage()
{
if (!currentlyAnimating)
{
//Begin the animation.
foreach (DataGridViewRow row in this.dgv_users.Rows)
{
if (row.IsNewRow == false)
{
Image img = imglist.Images["waiting.gif"];//row.Cells[IMAGE_COL_INDEX].Value as Image;
if (img != null)
{
ImageAnimator.Animate(img, new EventHandler(this.OnFrameChanged));
}
}
}
currentlyAnimating = true;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
//Force a call to the Paint event handler.
this.dgv_users.Invalidate();
}
private void dgv_users_Paint(object sender, PaintEventArgs e)
{
//Begin the animation.
AnimateImage();
//Update the frames. The cell would paint the next frame of the image late on.
ImageAnimator.UpdateFrames();
}
But it's now showing Animated image..rather no image is displyed :(
So, where I am doing wrong..Can u help me?