简体   繁体   English

如何使用 GridView 添加图像?

[英]How to add image using GridView?

I have a problem I would like to display images using GridView, but it is not working.我有一个问题,我想使用 GridView 显示图像,但它不起作用。 Images are upluded already图片已经上传

Here is a code这是一个代码

<asp:Gridview id="zbozi" runat="server"  AutoGenerateColumns="False" OnRowCommand="zbozi_RowCommand1" OnSelectedIndexChanged="zbozi_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="id_pocitace" HeaderText="ID" />
         <asp:BoundField DataField="nazev" HeaderText="Název" />
             <asp:BoundField DataField="cena" HeaderText="Cena" />
        
               HERE I WOULD LIKE TO PUT IMAGES


              <asp:BoundField DataField="popis" HeaderText="Popis" />  

    </Columns>   
</asp:Gridview>

This is Cs这是 Cs

public partial class online : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["idecko"] == null)
        {
            Response.Redirect("login.aspx");

        }
        uziv.Text = Convert.ToString(Session["uziv"]);
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["logins"].ConnectionString);
        conn.Open();

        SqlCommand sqlDa = new SqlCommand("select * from pocitace", conn);
        var rdr = sqlDa.ExecuteReader();
        zbozi.DataSource = rdr;
        zbozi.DataBind();

Image of visual studio, folders etc.. for understanding the situation:用于了解情况的视觉工作室、文件夹等图像:

视觉工作室、文件夹等的图像。用于了解情况

To show image in GridView, you need to create a column with "ImageField".要在 GridView 中显示图像,您需要创建一个带有“ImageField”的列。

<asp:GridView runat="server" ID="testGridView" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:ImageField DataImageUrlField="Image" HeaderText="Image"></asp:ImageField>
    </Columns>
</asp:GridView>

Then access the image path in the database directly and bind the data from database like:然后直接访问数据库中的图像路径并绑定数据库中的数据,如:

string constr = @"connection string";
con.Open();
da = new SqlDataAdapter("select * from TestTable", con);
ds = new DataSet();
da.Fill(ds);
testGridView.DataSource = ds;
testGridView.DataBind();

dont upload binary or like this in sql just upload the path and get the path不要在sql中上传二进制文件或类似的文件,只需上传路径并获取路径

add this column添加此列

<asp:ImageField DataImageUrlField="image_Path" HeaderText="Image"></asp:ImageField> <asp:ImageField DataImageUrlField="image_Path" HeaderText="Image"></asp:ImageField>

and set column name İmage_Path nvarchar in sql并在 sql 中设置列​​名 İmage_Path nvarchar

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

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