在GridView控件中显示图像

放一个 GridView 放上一个ImageField 来显示图像
前台代码

<table border="0" cellpadding="0" cellspacing="0" style=" 133px; height: 101px">
            <tr>
                <td style=" 100px">
                    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="215px" Width="297px" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:ImageField AlternateText="员工头像" HeaderText="员工头像" DataImageUrlField="photo">  // 绑定数据
                            </asp:ImageField>
                        </Columns>
                        <RowStyle BackColor="#EFF3FB" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                </td>
            </tr>
        </table>

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        string sql = "select * from tb_user";
        SqlDataAdapter sda = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds,"tb_user");
        DataRowView drv = ds.Tables["tb_user"].DefaultView[0];
        this.GridView1.DataSource = drv;
        this.GridView1.DataBind();
        this.GridView1.Columns[0].HeaderImageUrl =Convert.ToString(drv["photo"]);
//此处用到了HeaderImageUrl 来从数据库里得到图像的路径来显示图片


    }
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
    }

原文地址:https://www.cnblogs.com/ivy/p/1216108.html