This is a migrated thread and some comments may be shown as answers.

Show image in cell using Bound Data

1 Answer 229 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 2
Bob asked on 11 Jul 2012, 10:26 PM
Hi there-

I hope this is an easy one...

I have a gridView that is bound to a stored procedure.  One of the columns is of type GridViewImageColumn.  The data returned from the proc is of Integer type.  What I'm looking for is comparing the value (0 or greater than 0), whereas if 0, hide the image, if greater than 0, show the image.

So, given that, my first question is, how do I associate or add the image/png for the column/cell and how and when do I interrogate the bound data to show the image if greater than 0.

Hope this makes sense. 

Thanks
Bob

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 13 Jul 2012, 03:54 PM
Hi Bob,

I assume that your images do not come from the data base and the data which comes from there just determines whether an image should be shown or not. Let's say that the integer value is stored in a column called "ImageVisibility" and that the image column is called "ImageColumn". First, you need to set your images as shown below:
Image img = WindowsFormsApplication3.Properties.Resources._2RibbonMenuOpenMagenta;
 
for (int i = 0; i < this.radGridView1.Rows.Count; i++)
{
    this.radGridView1.Rows[i].Cells["ImageColumn"].Value = img;
}

Then, you should check the integer value in the CellFormatting event and depending on it, you should hide/show the associated image. Currently, the only way to hide an image is by setting the Image property to null:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "ImageColumn")
    {
        if (e.CellElement.RowInfo.Cells["ImageVisibility"].Value != null)
        {
            if ((decimal)e.CellElement.RowInfo.Cells["ImageVisibility"].Value > 0)
            {
                e.CellElement.Image = img;
            }
            else
            {
                e.CellElement.Image = 0;
            }
        }
    }
}

I hope this helps.

Greetings,
Nikolay
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Bob
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Share this question
or