2012-09-02 79 views
1

我有一個rowdatabound方法是兩個gridviews常見的。這個方法的一部分任務是爲gridview的最後一列賦值。gridview id在rowdatabound

gridview是相同的,但是這些值不同於兩個gridviews。所以我需要檢查一下是否將第一個gridview設置爲第一個gridview或第二個GridView。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     //Here i need to check which gridview (Gridview1 or Gridview2) 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      { 
       int CellValue = Convert.ToInt32(e.Row.Cells[0].Text); 

       if (CellValue == 1) 
        e.Row.Cells[7].Text = "" + patchWeekTwo[0] + "t"; 
       else if (CellValue == 2) 
        e.Row.Cells[7].Text = "" + patchWeekTwo[1] + "t"; 
       else if (CellValue == 3) 
        e.Row.Cells[7].Text = "" + patchWeekTwo[2] + "t"; 
       else if (CellValue == 4) 
        e.Row.Cells[7].Text = "" + patchWeekTwo[3] + "t"; 
       else 
        e.Row.Cells[7].Text = "" + patchWeekTwo[4] + "t"; 
      } 
     } 
    } 

回答

1

您可以檢查是否senderGridView1GridView2

if(sender == GridView1){} 
else{} 

注意,如果GridView1聲明在頁面的頂部,它不是一個人的孩子的NamingContainers這僅適用。然後,你可以檢查ID:

var grid = (GridView)sender; 
if(grid.Id == "GridView1"){} 
else{} 
1

我想這應該這樣做

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    GridView gv = (GridView)sender; 
    if(gv.ID == "gv1") 
     //do this 
    else 
     //do that 
} 
相關問題