c# - add image to datatable or gridview -


i have datatable bind gridview. columns variable i'd take advantage of autogeneratedcolumns. i'd bind image in condtions. need do?

void gridview1_rowcreated(object sender, system.web.ui.webcontrols.gridviewroweventargs e) {     datarowview drv = e.row.dataitem datarowview;     if (drv != null)         drv[1] = new htmlimage() { src = "add.png" }; } 

it sounds autogeneratedcolumns property won't here because column types apply whole gridview; not calculated per-row.

you might able use templatefield databinding conditionally format field each row without writing code.

if doesn't done you, suppose have write code. bear in mind rowcreated event fires (event on postback) when row created, give non-null dataitem (e.row.dataitem) when gridview goes datasource databinding; if gridview has cached rendered state (in viewstate), data item null. @ point, able access row's primary key fields doing this: var keys = mygridview.datakeys[rowindex]; (the primary key fields determined value give gridview's datakeynames property, , stored in viewstate can access them on postback.)

you careful when modifying column type of databoundfield (as fields are); since rowdatabound event happens after rowcreated event, manual changes content of row/cell make in rowcreated event handler are going clobbered databinding when rowdatabound fired.

that said, rowdatabound event want.

the rowdatabound event give non-null dataitem, fires when real databinding happens (as opposed "binding" viewstate); typically event not fire @ on postbacks. that's ok, though, since gridview remember state you.

if must use code, should this:

//don't forget attach event handler, either in markup  //on gridview control, in code (say, in page_init event handler.) protected void gridview1_rowdatabound(object sender, system.web.ui.webcontrols.gridviewroweventargs e) {   //htmlimage gives plain-vanilla <img> tag in html.     //if need handle server side events (such click)   //for image, use system.web.ui.webcontrols.image control   //instead.   htmlimage img = new htmlimage() { src = "path/to/image.jpg" };   e.row.cells[1].controls.add(img); } 

but, seriously, check out templatefield first.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -