Bob H
Bob H

Reputation: 51

How to display dynamically generated images in asp.net gridview?

I have a class (POCO) that has an Image property. I use a List<> of this class as the datasource for a gridview control. Most of the properties are grabbed from the database. However, I am dynamically generating a barcode (System.Drawing.Image) in my code behind and populating the Image property in the POCO with the result prior to databinding. It is NOT possible for me to store the images on the database or the filesystem. How do I go about displaying the barcodes in the Gridview control? When I try, nothing shows and the images generate 404 errors. The code for the gridview is:

<asp:GridView runat="server" ID="gvInventoryDetail" CssClass="centerpage" 
    style="width: 700px;" ItemType="DisplayModels.InventoryDetailItem" 
    DataKeyNames="InventoryId" AutoGenerateColumns="False"
    AllowPaging="True" PageSize="11" ShowHeaderWhenEmpty="True">
    <Columns>
       <asp:BoundField HeaderText="Barcode" DataField="Barcode"/>
       <asp:TemplateField HeaderText="Image">
           <ItemTemplate >
               <asp:Image ID="BarcodeImage" runat="server"
                   Height="150px" Width="80px" />
           </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField HeaderText="Comments" DataField="Comments"/>
       <asp:BoundField HeaderText="Status" DataField="Status"/>
       <asp:BoundField HeaderText="Broken?" DataField="IsBroken"/>
  </Columns>
  <SelectedRowStyle BackColor="#89d8f6" Font-Bold="True"/>
</asp:GridView>

Any help is MUCH appreciated!

Upvotes: 1

Views: 851

Answers (1)

Elim Garak
Elim Garak

Reputation: 1817

I had a similar issue when generating pie charts using another 3rd party project.

What I did was the following:

  1. Keep the Image Tag in your Grid.
  2. Set that Image Tag to call an ASPX Page in your site, passing what ever params you need on the URL to generate the barcode image.
  3. In that new ASPX do the following
    • response.clear()
    • response.binarywrite() of the image.

If you browse that URL you will see the image in the browser, which means, that URL would also show the image in your Image Tag in your grid.

Upvotes: 2

Related Questions