Sandeep Kumar

Sandeep Kumar

  • 1.1k
  • 683
  • 60.6k

How to bind image in Gridview Quickly

Sep 13 2023 9:27 AM

Image Path coming from DATA BASE but taking to much some time binding some time not binding ,when data come from server

<asp:GridView ID="grdSafety" runat="server" AutoGenerateColumns="false" Width="100%"
    PageSize="5" CssClass="table-grid">
    <Columns>
        <asp:TemplateField HeaderText="SRL NO">
            <HeaderStyle Width="20px" HorizontalAlign="Center" />
            <ItemStyle Width="20px" HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:Label ID="lbSrlno" runat="server" SkinID="GridLabel" Text='<%#Eval("SrlNo")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText=" AWARD NAME">
            <HeaderStyle Width="170px" HorizontalAlign="Center" />
            <ItemStyle Width="170px" HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:Label ID="lbAwardName" runat="server" SkinID="GridLabel" Text='<%#Eval("AwardName")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CATEGORY">
            <HeaderStyle Width="80px" HorizontalAlign="Center" />
            <ItemStyle Width="80px" HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:Label ID="lbAwardCategory" runat="server" SkinID="GridLabel" Text='<%#Eval("AwardCategory")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Month" Visible="false">
            <HeaderStyle Width="50px" HorizontalAlign="Center" />
            <ItemStyle Width="50px" HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:Label ID="lbMonth" runat="server" SkinID="GridLabel" Text='<%#Eval("Month")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="YEAR">
            <HeaderStyle Width="50px" HorizontalAlign="Center" />
            <ItemStyle Width="50px" HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:Label ID="lbYear" runat="server" SkinID="GridLabel" Text='<%#Eval("Year")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="LOGO">
            <HeaderStyle Width="60px" HorizontalAlign="Center" />
            <ItemStyle Width="60px" HorizontalAlign="Center" />
            <ItemTemplate>
             
                <asp:ImageButton ID="imgBtndownloads" runat="server" Width="50" Height="50" ImageUrl='<%#PreviewImage(Eval("AwardPhotoPath").ToString())%>'
                    OnClientClick="return LoadDiv(this.src);" Visible='<%# Eval("AwardPhotoPath").ToString() != "" ? true : false %>'>
                </asp:ImageButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <table align="center">
            <tr>
                <td style="color: Red; font-family: Verdana; font-size: 11px; text-align: center;">
                    No results found.
                </td>
            </tr>
        </table>
    </EmptyDataTemplate>
</asp:GridView>
protected string PreviewImage(string filepath)
{
    string imageUrl = "";
    if (System.IO.File.Exists(filepath))
    {
        FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        br.Close();
        fs.Close();
        string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
        imageUrl = "data:image/png;base64," + base64String;
    }
    return imageUrl;
}

CS

DataTable dt = AwardInfoClass.GetAwardPortalWise(varAwardPortal, PageNo, PageSize, out OutputVal);

grdSafety.DataSource = dt;
if (dt.Rows.Count == 0)
{
    Pagingdata.Visible = false;
    grdSafety.DataSource = null;
    grdSafety.DataBind();
  
}
else
{
    Pagingdata.Visible = true;
}

grdSafety.DataBind();

 


Answers (3)