'Dynamically add Image into asp.net c# gridview

I would like to display image inside a gridview. My gridview is being filled like This: Front:

        <asp:GridView 
    ID="GridViewProduct"
    runat="server" 
    CellPadding="4" 
    GridLines="Horizontal"
    AutoGenerateColumns="False"
    width="1020"
    onrowcommand="GridViewCase_RowCommand">
        <Columns>
            <asp:BoundField DataField="ID" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="IDKolonne" HeaderText="Id" ItemStyle-Width="40px" HeaderStyle-CssClass="header"/>
         <asp:ImageField DataImageUrlField="idimg" NullImageUrl="images/thumbs/2.jpg">
            <ControlStyle Height="40px" Width="40px" />
            </asp:ImageField>

            <asp:buttonfield buttontype="Image" ItemStyle-HorizontalAlign="Center"  ImageUrl="~/img/trash.png" commandname="Del" text="Slet Produkt" HeaderText="Slet Produkt"/>
            <asp:buttonfield buttontype="Image" ItemStyle-HorizontalAlign="Center"  ImageUrl="~/img/change.png" commandname="Select" text="Se / Ret" HeaderText="Se / Ret"/>
        </Columns> 
    </asp:GridView>

As you see I am able of inserting image static. I would like to ad image from my db referring to ID. My gridview is builded like this:

    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("idimg", typeof(System.Drawing.Image));

    DataTable dt = new DataTable();
    dt = galFac.getCurrentUsersElements(3);

    if (dt.Rows.Count > 0)
    {
        foreach (DataRow item in dt.Rows)
        {
            table.Rows.Add(item["Id"]);
        }

        GridViewProduct.DataSource = table;
        GridViewProduct.DataBind();

        GridViewProduct.UseAccessibleHeader = true;            
    }

How will this be possible? Thanks.



Solution 1:[1]

You just need to pass "image path" as string parameter to gridview datasource and use the following asp:Image on your gridview

<asp:Image id="abc" ImageUrl =<%#Eval("column_name_image"))%>

the aspx.cs code is like this:

DataTable table = new DataTable();
table.Columns.Add("ID", typeof(System.Int32));
table.Columns.Add("column_name_image", typeof(string));

.....
.....
.....

GridViewProduct.DataSource = table;
GridViewProduct.DataBind();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1