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...

C#
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        Sold_Update();

    }



C#
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;
        cmd.Connection.Open();
        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")
{
e.Row.Cells[1].Controls.Clear();
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
{
e.Row.Cells[1].Controls.Add(img);
}

}
Posted
Updated 18-Nov-11 0:55am
v3

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.

C#
void productsGridView_RowDataBound(object sender,
  GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // determine the value of the UnitsInStock field
        int unitsInStock =
         Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
         "UnitsInStock"));
        if (unitsInStock == 0)
            // color the background of the row Red
            e.Row.BackColor = Color.Red;
        else
            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