通過(guò)e.Row實(shí)現(xiàn)GridViewRow訪問(wèn)單元格
我們需要訪問(wèn)GridViewID.Rows[index]來(lái)訪問(wèn)index對(duì)應(yīng)的那一行,GridViewID.Rows[index].Cells[index]來(lái)訪問(wèn)某一單元格.然而當(dāng)RowDataBound事件觸發(fā)時(shí),GridViewRow卻沒(méi)有添加到Rows集合中, 因此我們不能在RowDataBound事件處理中通過(guò)當(dāng)前GridViewRow實(shí)例
取而代之,我們可以通過(guò)e.Row來(lái)訪問(wèn)。為了高亮某一行我們用下面的代碼
- e.Row.BackColor = System.Drawing.Color.Yellow;
我們還可以通過(guò)cSSClass取得同樣的效果(推薦)
- protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- // Make sure we are working with a DataRow
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- // Get the ProductsRow object from the DataItem property...
- Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
- if (!product.IsUnitPriceNull() && product.UnitPrice < 10m)
- {
- e.Row.CssClass = "AffordablePriceEmphasis";
- }
- }
- }
GridViewRow: 所需要的行用高亮黃色顯示
【編輯推薦】