Hey,

im trying to dynamically add an image to a gridview for each row if it meets a condition.

DataTable dtSub = new DataTable();
dtSub.Columns.Add("ID", Type.GetType("System.String"));
dtSub.Columns.Add("FirstName", Type.GetType("System.String"));
dtSub.Columns.Add("LastName", Type.GetType("System.String"));
dtSub.Columns.Add("Tutor", Type.GetType("System.String"));
dtSub.Columns.Add("test", typeof(System.Web.UI.WebControls.Image)); //not sure if this is correct??
DataSet ds = new DataSet();
ds.Tables.Add(dtSub);
Session["myds"] = ds;



foreach (var item in GetDataItem)
{
   int studIDconflict = item.stud_id; //get the id


    if (count != studIDconflict)//compare both ids
       {
             System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
             img.ResolveUrl("~/images/star.png");
             img.ToolTip = "dd";
             dtSub.Rows[dtSub.Rows.Count - 1]["test"] = img;

            //dtSub.Rows[dtSub.Rows.Count - 1]["test"] = ResolveUrl("~/images/star.png");   
            //HyperLink planLink = (HyperLink)dtSub.Rows[dtSub.Rows.Count - 1]["test"];
            //planLink.ImageUrl = string.Format("~/Images/{0}.jpg", ID);
            //planLink.ImageUrl = ResolveUrl("~/images/star.png");
            // planLink.ToolTip = "le testing";
            //planLink.ImageUrl = string.Format("~/images/star.png");
        }
}

I have tried so many different things, its possible if i add an <asp:image> to the html but if the condition is not met the image control still apears but without the image. This is why i want the control to be created only is the condition is met.

im unsure how to link the new instance of an image control to the gridview columns, most things iv found online are binding images via the database. Anyone able to help?

You need to add the Image control into GridView's TemplateField or just use ImageField.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.