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

Bind GridViewImageColumn

1 Answer 163 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vishal
Top achievements
Rank 1
Vishal asked on 31 Oct 2012, 08:32 AM
Dear Support,

How to bind GridViewImageColumn to a collection. I have a collection, which contains _itemImage.Image (image), and _itemImage.ImageName (string)

I wish to have GridViewImageColumn with Image, and Text written next to it in the column

Ex:
Image(_itemImage.Image)  ImageName(_itemImage.ImageName)



1 Answer, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 02 Nov 2012, 02:57 PM
Hello Vishal,

Thank you for writing.

If you want to show both the image and text in a cell in GridViewImageColumn, you can use the CellFormatting event to set the text and the text/image position. Here is an example
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        List<Element> list = new List<Element>();
        list.Add(new Element() {ImageName ="row 1", Image= Image.FromFile(@"C:\Program Files (x86)\Telerik\RadControls for WinForms Q3 2012\Examples\QuickStart\Resources\1RibbonMenuNew2.png")});
        list.Add(new Element() {ImageName ="row 2", Image= Image.FromFile(@"C:\Program Files (x86)\Telerik\RadControls for WinForms Q3 2012\Examples\QuickStart\Resources\2RibbonMenuOpenMagenta.png")});
        list.Add(new Element() {ImageName ="row 3",Image= Image.FromFile(@"C:\Program Files (x86)\Telerik\RadControls for WinForms Q3 2012\Examples\QuickStart\Resources\3RibbonMenuSave2.png")});
 
        radGridView1.DataSource = list;
        radGridView1.AutoSizeRows = true;
 
        radGridView1.Columns["ImageName"].IsVisible = false;
        radGridView1.Columns["Image"].Width = 100;
 
        radGridView1.CellFormatting += radGridView1_CellFormatting;
    }
 
    void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        GridImageCellElement imgCell = e.CellElement as GridImageCellElement;
         
        if (imgCell != null && e.Row is GridViewDataRowInfo)
        {
            Element view = e.Row.DataBoundItem as Element;
            imgCell.Text = view.ImageName;
            imgCell.TextImageRelation = TextImageRelation.ImageBeforeText;
        }
    }
 
    class Element
    {
        public  string ImageName{ get; set; }
        public Image Image{ get; set; }
    }
}

I hope this helps. Should you have any other questions, do not hesitate to contact us.

Greetings,
Anton
the Telerik team
Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Vishal
Top achievements
Rank 1
Answers by
Anton
Telerik team
Share this question
or