简体   繁体   English

动态添加图像到asp.net c# gridview

[英]Dynamically add Image into asp.net c# gridview

I would like to display image inside a gridview.我想在 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.我想从我的数据库中引用 ID 的广告图像。 My gridview is builded like this:我的 gridview 是这样构建的:

    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.谢谢。

You just need to pass "image path" as string parameter to gridview datasource and use the following asp:Image on your gridview您只需要将“图像路径”作为字符串参数传递给 gridview 数据源并在 gridview 上使用以下 asp:Image

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

the aspx.cs code is like this: aspx.cs 代码是这样的:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM