Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i want to add image in grid view at run time. I am using the following code. its showing error pleas help me to solve the problem .

XML
<asp:GridView ID="gvSpecialist" runat="server" AutoGenerateColumns="false" Width="965px"
     
onpageindexchanging="gvSpecialist_PageIndexChanging" >
        
        <columns>
        <asp:TemplateField HeaderText="Doctor Name">
            <itemtemplate>
                <asp:LinkButton ID="lnkNames" runat="server" ForeColor="black" Text='<%#Eval("Name")%>' CommandName="Name" CommandArgument='<%# Eval("Name") %>'>
            </itemtemplate>
            <itemstyle width="250px" cssclass="gridcol" />
            <HeaderStyle  HorizontalAlign="Center" />

       <asp:TemplateField  >
           <itemtemplate>
           <asp:Image ID="Image1" runat="server"  ImageUrl='<%# BindImage(Eval("online")) %>' ImageAlign="Left" />
         
        </itemtemplate>
     
        </columns>
        <footerstyle backcolor="#D4A97F" font-bold="True" forecolor="White" />
        <pagerstyle backcolor="#D4A97F" forecolor="Black" horizontalalign="Center">
            Wrap="False" Font-Bold="True" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#d4a97f" Font-Bold="True" ForeColor="black" 
            Height="30" Wrap="False" Width="75" />
        <editrowstyle backcolor="#999999" />
        <alternatingrowstyle backcolor="#E2DED6" forecolor="black" />


In code behind:
C#
public string BindImage(string online)
{
    
    if (online == "Y")
    {
        string imgpath = "~/images/Greenflag.png";
        return imgpath;
    }
    else
    {
        string imgpath = "~/images/Redflag.png";
        return imgpath;
    }

}

but it showing error as cannot convert object to string..

please help

thanks
parithi
Posted
v2
Comments
RelicV 5-Jun-13 7:29am    
Try replacing the BindImage(string online) with BindImage(object online) or BindImage(var online) and use type casting to convert into string.
Nandakishore G N 5-Jun-13 8:41am    
how are you binding data and image to grid? paste it the code behind

Hi

I think you have to do the convert the parameter from object to string.

So your code need to be altered as below

XML
<asp:GridView ID="gvSpecialist" runat="server" AutoGenerateColumns="false" Width="965px"

onpageindexchanging="gvSpecialist_PageIndexChanging" >

        <columns>
        <asp:TemplateField HeaderText="Doctor Name">
            <itemtemplate>
                <asp:LinkButton ID="lnkNames" runat="server" ForeColor="black" Text='<%#Eval("Name")%>' CommandName="Name" CommandArgument='<%# Eval("Name") %>'>
            </itemtemplate>
            <itemstyle width="250px" cssclass="gridcol" />
            <HeaderStyle  HorizontalAlign="Center" />

       <asp:TemplateField  >
           <itemtemplate>
           <asp:Image ID="Image1" runat="server"  ImageUrl='<%# BindImage(Eval("online").ToString()) %>' ImageAlign="Left" />

        </itemtemplate>

        </columns>
        <footerstyle backcolor="#D4A97F" font-bold="True" forecolor="White" />
        <pagerstyle backcolor="#D4A97F" forecolor="Black" horizontalalign="Center">
            Wrap="False" Font-Bold="True" />



I hope this one will help you
 
Share this answer
 
Comments
parithi vr 6-Jun-13 0:27am    
Thanks it is really helpful.
Prince Sagayaraj 6-Jun-13 3:01am    
Cheers!!!!!!!!!!!!!! :-) .. You are welcome...
XML
<asp:TemplateField HeaderText="Thumbnail Image">
                               <ItemTemplate>                                   <asp:ImageButton ID="imgThumbnail" runat="server" ImageUrl='<%# GetThumbnail(Convert.ToString(DataBinder.Eval(Container.DataItem,"ImagePath"))) %>'       Width="60px" Height="60px" AlternateText="Thumbanail Image " ToolTip='<%# GetThumbnail(Convert.ToString(DataBinder.Eval(Container.DataItem,"ImagePath"))) %>' />
                               </ItemTemplate>
                           </asp:TemplateField>



C#
public string GetThumbnail(string strpath)
    {
        if (!string.IsNullOrEmpty(strpath))
        {
            string str1 = "http://virtual path of images folder/"> + "/" + strpath.ToString();
        
            return str1;
        }
        else
        {
            return "";
        }
    }
 
Share this answer
 
Bind image field with the path of image and then update path like this:

C#
public void dynimg()
{
    SqlCommand cmd1 = default(SqlCommand);
    SqlConnection conn1 = default(SqlConnection);
    dynamic sql1 = "update Rx set url='images/1.gif' where motion=1";
    conn1 = new SqlConnection("Data Source=174.142.75.238;Initial Catalog=TDS;User ID=gac;Password=ffffff");
    cmd1 = new SqlCommand(sql1, conn1);
    conn1.Open();
    cmd1.ExecuteNonQuery();
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900