Search code examples
c#asp.net.netgridviewbase64url

GridView OnRowData Bound, Img_Data from SQL Base64string without templatefild


On my page i have a Gridview fildet over SQl Auto witout Template Filds. All setting are code behind. ondatabound

Now i have a Base64 String in the sql table and gridview dont schow image. i see the base64string in the result.

How i can convert the string to image in code behind section witout TemplateFilds in aspx. Templatefild is not able Gridview is dinamicel over multible combination of databases. Just the firs 2 columns are staic.

ASPX

<asp:GridView ID="mergeresult" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" OnRowCreated="mergeresult_OnRowCreated"
                                OnRowDataBound="mergeresult_OnRowDataBound" BorderWidth="1px" CellPadding="2" 
                                ForeColor="Black" GridLines="Both" autogeneratedcolumns=false>
                            <AlternatingRowStyle BackColor="PaleGoldenrod" />
                            <FooterStyle BackColor="Tan" />
                            <HeaderStyle BackColor="Tan" Font-Bold="True" />
                            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                            <SortedAscendingCellStyle BackColor="#FAFAE7" />
                            <SortedAscendingHeaderStyle BackColor="#DAC09E" />
                            <SortedDescendingCellStyle BackColor="#E1DB9C" />
                            <SortedDescendingHeaderStyle BackColor="#C2A47B" />
                            <HeaderStyle Font-Size="Small" Font-Underline="true" />
                            <RowStyle Font-Size="12px" />

                         </asp:GridView>

C#

        protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowIndex >= 0)
        {
                int totalRow = mergeresult.Rows.Count;
                int totalColumns = mergeresult.Columns.Count;
 
           for (int i = 0; i < totalRow + 1; i++)
              {
                e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
                // Image Converting ?? Field e.Row.Cells[1] have the base64 string inside.



              }
                 
         }
    }








             
}

DataBind

  protected void run_Click(object sender, EventArgs e)

    { 
       // Dynamicel results from SQL Databases


        mergeresult.DataSource = dt1;
        mergeresult.DataBind();
}

Solution

  • manipulate your SQL so the output is like below, How to display Base64 images in HTML

     <div>
     <p>Taken from wikpedia</p>
     <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
    </div>