Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i want to show image in grid view and img come from sql server .how it 's posssible plz give me code
Posted
Updated 23-Jan-12 10:22am
v2
Comments
[no name] 23-Jan-12 16:22pm    
It is very rude around here to just ask for code without showing you have made any attempt.
Manfred Rudolf Bihy 23-Jan-12 17:30pm    
Before pestering us with this have you tried to google it? Cherchez le code!
janwel 24-Jan-12 0:48am    
There is so many tutorials on the net. One is you could you handler the next one is that you could create it programatically

Hi Please use following code

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCell tCell = new TableCell();
            // create image 
            Image img = new Image();
            img.ImageUrl = "subheader.jpg";
            // add the image to the cell
            tCell.Controls.Add(img);
            
            GridView gView = (GridView)sender;
            // set the colspan to occupy the other cells in the row
            int colSpan = gView.Columns.Count;
            tCell.Attributes["ColSpan"] = colSpan.ToString(); 
 
            GridViewRow gRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
            // add the cells to the gridviewrow
            gRow.Cells.Add(tCell);
 
            Table tbl = (Table)e.Row.Parent;
 
            // set the pagesize initially to the pagecount/2
            // in our case it is 10/2 = 5. So the first image will 
            // displayed after the 5th row.
            if(pgSize == 0)
                pgSize = GridView1.PageCount / 2;
 
            // This step is performed so that we can display the image only after every 
            // 5, 15, 25 ,35 rows and so on ...
            // The logic is not perfect but will give you the idea
            if (Convert.ToDouble(e.Row.DataItemIndex + 1) / Convert.ToDouble(pgSize) == 1.0)
            {
                tbl.Controls.AddAt(gView.Controls[0].Controls.Count, gRow);
                // add 10 to the pgsize so that the image can be displayed
                // at rows 5, 15, 25 and so on..
                pgSize = pgSize + 10;
            }
        }
    }

Regards
Sujeet
 
Share this answer
 
v2
See this CP article, this may helps you

Displaying Images from a Database in a GridView[^]


Thanks
--RA
 
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