Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Show Animated Gif in DataGridView TextAndImageColumn
 

Show Animated Gif in DataGridView TextAndImageColumn

Hi,
        In a DatagridView control, I have a coumn as TextAndImageColumn. In this column I want to show a gif image whose frames are same.
By referring http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0d9e790e-6816-40e7-96fe-bbf333a4abc0/ . article, I tried to implement the animated image in the TextAndImageColumn but no suceess :(

My code snippets :

 
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();
}
Even in AnimateImage() function,
Image img = imglist.Images["waiting.gif"

];//row.Cells[IMAGE_COL_INDEX].Value as Image;
Here right now I have hardcoded the image waiting .gif ... instead of that I want to get the TextImagecolumn image and assign it it img variable.

Thanks In advance !
Tejaswini Prashant J  Sunday, October 04, 2009 7:03 AM
Hi Tejaswini,

Based on my understanding, it is not appropriate to set the image of the column in the CellPainting event handler. You need to move the code to the Load event handler of the Form. Please use the Insert Code Block button to post the complete code of the TextAndImageColumn column to help us test.
The link ought to be: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0d9e790e-6816-40e7-96fe-bbf333a4abc0

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 6:32 AM
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?
Tejaswini Prashant J  Wednesday, October 07, 2009 8:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview Combobox AutoSizeMode
• DataGridView Delete Row
• vb.net datagridview
• DataGridView Two-Way Binding
• Refresh databound Combobox
• Building a custom cell/column for the DataGridView
• Master child sub-child binding on 2 forms
• Combobox Problem in DataGridView - calling all gurus
• Griview Problem
• DataGridView Bug?