Oliver Catipon
Oliver Catipon

Reputation: 83

How to change the path of picture into picture in GridView?

I need to show Prod_Picture into picture, not a path text. I can't set property in GridView because I bound this code to GridView and no column has been create there.

    protected void Page_Load(object sender, EventArgs e)
    {
        var item = from c in db.Products
                   join o in db.ProductCategories
                   on c.ProdC_ID equals o.ProdC_ID
                   select new
                   {
                       ProductID = c.Prod_ID,
                       ProductName = c.Prod_Name,
                       Price = c.Prod_Price,
                       Amount = c.Prod_Amount,
                       Picture = c.Prod_Picture,
                       ProductDetail = c.Prod_Detail,
                       ProductCategory = o.ProdC_Name
                   };
        GridView1.DataSource = item;
        GridView1.DataBind();
    }

Here source code for GridView:

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

Please help !!

Upvotes: 0

Views: 140

Answers (2)

pravprab
pravprab

Reputation: 2293

I think you are storing image Url as Prod_Picture. in That case

<asp:BoundField DataField="Prod_Name" HeaderText="Prod_Name" />

<asp:BoundField DataField="Prod_Price" HeaderText="Prod_Price" />

<asp:TemplateField HeaderText="Image">

 <ItemTemplate>

 <asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Prod_Picture")) %>' runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

<RowStyle BackColor="White" ForeColor="#330099" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

</asp:GridView>

this will work

Upvotes: 1

Stuart
Stuart

Reputation: 699

You can refer to the topic below:

Displaying Images from a Database in a GridView

Perform the following actions for the gridview:

enter image description here

  1. Uncheck the Auto-generate field
  2. Use two BoundFields for UserName and Country
  3. Set the Header Text and DataField (field name in database)
  4. Use ImageField for the image and set DataImageUrlField = ImageFieldName
  5. Click OK

Upvotes: 2

Related Questions