通過e.Row實現GridViewRow訪問單元格
作者:xwang
本文簡單介紹了如何通過e.Row實現GridViewRow訪問單元格。
我們需要訪問GridViewID.Rows[index]來訪問index對應的那一行,GridViewID.Rows[index].Cells[index]來訪問某一單元格.然而當RowDataBound事件觸發時,GridViewRow卻沒有添加到Rows集合中, 因此我們不能在RowDataBound事件處理中通過當前GridViewRow實例
取而代之,我們可以通過e.Row來訪問。為了高亮某一行我們用下面的代碼
- e.Row.BackColor = System.Drawing.Color.Yellow;
我們還可以通過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: 所需要的行用高亮黃色顯示
【編輯推薦】
責任編輯:book05
來源:
博客堂