How Popular Image in Gridview?

Asked

Viewed 298 times

0

I’m doing an app on Asp.NET, with Entity Framework and using the database Oracle and here’s the thing I’m uploading the image by a button and saving in the database with the type BLOB until then all right, but I need the image to appear on the screen along with information from another table called PRODUTOS, I was thinking popular line by line with DataTable, but because I couldn’t.

Database with BLOB image

Banco de dados com a imagem BLOB

Entity

Entinty

Gridview with image

GridView com imagem

Gridview Rendered in browser

Gridview Renderizado no browser

private void carregarImagem()
{
        dt.Columns.Add("id");
        dt.Columns.Add("nome");
        dt.Columns.Add("valor_unit");
        dt.Columns.Add("imagem1");

        ProjetoContext bd = new ProjetoContext();
        List<PRODUTO> produtos = bd.PRODUTOes.ToList();
        List<IMG_PRODUTO> imagem = bd.IMG_PRODUTO.ToList();

        foreach (PRODUTO pr in produtos)
        {

            IMG_PRODUTO im = bd.IMG_PRODUTO.Find(pr.ID_IMAGEM);
            if (im != null && pr.ID_IMAGEM != 0)
            {
                //Image img = (Image)GridProdutos.Rows[1].Cells[4].FindControl
                byte[] byteImagem = im.IMAGEM;
                string img64Frente = Convert.ToBase64String(byteImagem);
                string imagemDataUrl = 
                  String.Format("data:image/png;base64, {0}", img64Frente);
                //img.ImageUrl = imagemDataUrl;
                dt.Rows.Add(pr.ID, pr.NOME, pr.VALOR_UNIT, imagemDataUrl);
            }
        }

        GridProdutos.DataSource = dt;
        GridProdutos.DataBind();

}

HTML CODE

        <asp:GridView ID="GridProdutos" runat="server" AutoGenerateColumns ="false" OnRowCommand ="GridProdutos_RowCommand" AllowPaging="true" OnPageIndexChanging="GridProdutos_PageIndexChanging" OnRowDataBound ="GridProdutos_RowDataBound">
        <columns>
            <asp:BoundField DataField ="id" HeaderText="Index" />
            <asp:BoundField DataField ="nome" HeaderText="Nome do Produto" />
            <asp:BoundField DataField ="valor_unit" DataFormatString="{0:c}" HeaderText="Valor Unitario" />
            <asp:TemplateField>
                <ItemTemplate>
                     <asp:Image ID="Image1" runat="server" Width="100" Height="100" />
                    <asp:Button ID ="btnAdiciona" runat="server" CommandName="AddItem" Text="Add Carrinho" CommandArgument='<%# Bind("id") %>'/>
                </ItemTemplate>
            </asp:TemplateField>

        </columns>
    </asp:GridView>
  • One could put the HTML code that was generated?

  • ready, I put the HTML

  • In the picture1 did not fail to pass the command to load the information. ?

  • 2

    Test there <asp:Image ID="Image1" runat="server" Width="100" Height="100" ImageUrl='<%# Bind("imagem1") %>' />

  • Yes, it gave kkkk, you can explain to me how you solved it ?

  • 1

    I looked at your code and was missing pass the information to the controller to load ... if you made the correct code, but at the time of calling on the screen did not pass the variable to load the items! if blz worked!

Show 1 more comment

1 answer

0


Well the command is very simple, code available.

Asp.net - Web Form
to upload

<asp:FileUpload ID="FileUp" runat="server" Width="348px" />


Insert into database - Backend

                    img.IMAGEM = FileUp.FileBytes;
                    img.DESCRICAO = cxNome.Text;
                    img.ID = TotalImg;
                    bd.IMG_PRODUTO.Add(img);

recover the image

                    byte[] byteImagem = im.IMAGEM;
                    string imagConvert = Convert.ToBase64String(byteImagem);
                    string imagemDataUrl = String.Format("data:imagem/png;base64, {0}", imagConvert);

Credit to @Virgilio Novic for the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.