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

Load image in cell when loading any database

1 Answer 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gord
Top achievements
Rank 1
Gord asked on 30 Jul 2011, 04:21 AM
Hi,

I have an app that loads any database and presents the data for the tables in a GridView. I do not define columns in the XAML since I load any database. I'd like to be able to determine when a column from the database contains images and display the images in the grid. Can you tell me how to do this? I see examples in the forms and KB but they all seem to create the columns in XAML since they know the database schema ahead of time.

Thanks,

Gord

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 04 Aug 2011, 03:06 PM
Hi Gord,

Indeed you are right that the Help article about the ImageColumn needs to be updated. Thank you for this point.

You may bind the GridViewImageColumn to a string or a byte[] data using the DataMemberBinding property. On your question,  in order to create and bind the column in code behind based on the database data, you will need an appropriate property to bind to.

For example in your ViewModel(that is the DataContext of the GridView) you will need a property for the Picture like so:

byte[] _Picture;
public byte[] Picture
{
get
{
    if (_Picture == null)
   {
        if (!String.IsNullOrEmpty(this.Photo))
        {
            this._Picture = // get the byte[] data from the DataBase
                 }
  }
}
return this._Picture;
}

Then in code behind you determine when a column from the database contains images
and based on that create an GridViewImageColumn and add it to the columns of the grid:

public MainPage()
       {
           InitializeComponent();
 
           if (showImageFromDb)
           {
               GridViewImageColumn imageColumn = new GridViewImageColumn();
               imageColumn.ImageHeight = 50;
               imageColumn.ImageWidth = 50;
               imageColumn.DataMemberBinding = new Binding("Picture");
               clubsGrid.Columns.Add(imageColumn);
           }
       }

 
I hope that this helps. Please let me know in case something is not clear.

Best wishes,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Gord
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or