Click here to Skip to main content
15,946,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts...

I am here to bind images to gridview dynamically.. What i need is i have to bind image dynamically with a count in database i.e. in plotdetails table if there is a 10 plots in a project then i have to bind the sold plots in red colour image in grid..

i wrote some code but i stuck in bind the images in gridview... here is my code...

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)


public DataSet Sold_Update()
        cmd = new SqlCommand("Select count(Plotid) from Mas_PlotDetails where Projid='" + DropDownList1.SelectedItem.Text + "'and Sold_Status=1", con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Mas_PlotDetails");
        return ds;        

What I need is to have to take the plot counts and bind the red colour image in grid if the plot is sold otherwise if the plot is not sold some other colour. I have to bind the no of counts plot to as image in grid.

I done the following code now... its shown the image.. but i should loop the image as per the count.. How to do this...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
DataRowView dr = (DataRowView)e.Row.DataItem;
string myflag = dr["Plotid"].ToString();
if (myflag != "0")
ImageButton img = new ImageButton();
img.ImageUrl = "~/Images/wchair.jpg";

for (int i = 0; i < ;i++ ) // Here how to loop the myflag so that the count is 10, i hav to display the 10 images

Updated 18-Nov-11 0:55am

1 solution

Bind your gridview normally, and format the rows inside RowDataBound event of gridview, this event is executed for each rows after binding the data to it.

void productsGridView_RowDataBound(object sender,
  GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        // determine the value of the UnitsInStock field
        int unitsInStock =
        if (unitsInStock == 0)
            // color the background of the row Red
            e.Row.BackColor = Color.Red;
            e.Row.BackColor = Color.Green;
Share this answer

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