如何在GridView中插入图像
问题描述:
我想在网格视图中显示图像,并且img来自sql server.它怎么可能给我代码
hi i want to show image in grid view and img come from sql server .how it ''s posssible plz give me code
答
请使用以下代码
Hi Please use following code
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;
}
}
}
问候
Sujeet
Regards
Sujeet
尝试
在ASP.Net中显示来自SQL Server数据库的图像GridView
http://www.dotnetfunda.com/articles/article1084-aving-images-into-the-database-in-aspnet-and-displaying-to-the-gridview-.aspx
try
Display images from SQL Server Database in ASP.Net GridView
http://www.dotnetfunda.com/articles/article1084-saving-images-into-the-database-in-aspnet-and-displaying-to-the-gridview-.aspx
请参阅此CP文章,这可能对您有帮助
在GridView中显示数据库中的图像 [^ ]
谢谢
--RA
See this CP article, this may helps you
Displaying Images from a Database in a GridView[^]
Thanks
--RA