Click to See Complete Forum and Search --> : Bind image to the datagrid


learner_7983
October 21st, 2004, 08:17 PM
I have a column Image in the table, data type varchar which hold the path to the actual image in my computer eg :- C:\Images\book.jpg.

When I try to display the result in the datagrid in web application it displays the path to me as C:\Images\book.jpg.
What I actually want is the actual picture instead of the full path.

I tried this code too:-
string img ="";
foreach(DataRow row in imageDataSet1.Tables["ImageTable"].Rows)
{
img =row[0].ToString();
//labISBN.Text = row[1].ToString();
//labTitle.Text = row[2].ToString();
}
If I say Image1.ImageUrl = img;
I can get the image but not in the data grid

If I say DataGrid1.DataBind();//other code omitted

It displays the full path not the actual image in that column
I couldn’t find options in the property builder of the datagrid

I’m using C# as the programming language.
Thanks in advance

mmetzger
October 23rd, 2004, 10:54 PM
There's a few ways to do this with varying degrees of "correctness"... It will help if you post your code to the page more than you are. With what's below, it looks like you're trying to iterate through and write the response html yourself. While you can do this, it pretty much removes the point of using a datagrid.

Normally to do this, you would have a DataGrid defined on your page with the appropriate BoundColumns. The simplest thing to do for this would be to create a TemplateColumn for the image and set the source equal to the Database column. You'll need to add a eventHandler for OnItemDataBound and it should work automatically.

Side Note: The image paths may cause you some issues depending on the web server you're using. Remember that unless you dynamically open / read / send the image data to the client making the request, the client will actually request the image based on the source listed. In other words, it may make a request such as:

GET C:\Images\book.jpg HTTP/1.1

You may want to either add replacement code on the image text to point to a more web friendly path or change the path in the database.