通过e.Row实现GridViewRow访问单元格


  本文标签:GridViewRow

  我们需要访问GridViewID.Rows[index]来访问index对应的那一行,GridViewID.Rows[index].Cells[index]来访问某一单元格.然而当RowDataBound事件触发时,GridViewRow却没有添加到Rows集合中, 因此我们不能在RowDataBound事件处理中通过当前GridViewRow实例

  取而代之,我们可以通过e.Row来访问  。为了高亮某一行我们用下面的代码

  1. e.Row.BackColor = System.Drawing.Color.Yellow;  

  我们还可以通过cSSClass取得同样的效果(推荐)   

  1. protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.  
  3.     {  
  4.  
  5.         // Make sure we are working with a DataRow  
  6.  
  7.         if (e.Row.RowType == DataControlRowType.DataRow)  
  8.  
  9.         {  
  10.  
  11.             // Get the ProductsRow object from the DataItem property...  
  12.  
  13.             Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;  
  14.  
  15.             if (!product.IsUnitPriceNull() && product.UnitPrice <  10m)  
  16.  
  17.             {  
  18.  
  19.                 e.Row.CssClass = "AffordablePriceEmphasis";  
  20.  
  21.             }  
  22.  
  23.         }  
  24.  
  25.     }  
  26.  

  所需要的行用高亮黄色显示 

  GridViewRow: 所需要的行用高亮黄色显示