【问题标题】:Dynamically add Image into asp.net c# gridview将图像动态添加到asp.net c# gridview
【发布时间】:2014-02-20 12:56:57
【问题描述】:

我想在 gridview 中显示图像。我的 gridview 正在像这样填充: 正面:

        <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>

如您所见,我能够插入静态图像。我想从我的数据库中发布关于 ID 的图片。我的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;            
    }

这怎么可能? 谢谢。

【问题讨论】:

  • 您是否将图像保存在数据库中或图像 URL 保存在数据库中,您使用的是什么数据库?
  • 您好,感谢您的快速回复。我保存了图片网址

标签: c# asp.net image gridview


【解决方案1】:

您只需将“图像路径”作为字符串参数传递给 gridview 数据源并在您的 gridview 上使用以下 asp:Image

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

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();

【讨论】:

  • 对不起,我是菜鸟。我不会将 Column 与 ID 绑定吗? : table.Columns.Add("abc", typeof(string));和。我要在 foreach 中为 生成一个字符串名称吗?我想asp图像控件将运行在=“服务器”。感谢您帮助我。
猜你喜欢
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多