c# - Why foreach loop fails at last row of gridview? -
i have put code color background of gridview cell# 14 if cell's text != "nbsp;" , work except last row. doesn't color last row isn't equal "nbsp;"
protected void grdviewcases_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { foreach (gridviewrow gr in grdviewcases.rows) { if (gr.cells[14].text != " ") { gr.cells[14].backcolor = color.red; ; gr.cells[14].forecolor = color.whitesmoke; } } } }
you need not loop rows in rowdatabound event, may use e
object reference each row
protected void grdviewcases_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { if (e.row.cells[14].text != " ") { e.row.cells[14].backcolor = color.red; ; e.row.cells[14].forecolor = color.whitesmoke; } } }
for more details check system.web.ui.webcontrols.gridview.rowdatabound
Comments
Post a Comment