2

我有一个 rowdatabound 方法,这对于两个 gridviews 很常见。此方法的部分任务是将值分配给 gridview 的最后一列。

网格视图相同,但值与两个网格视图不同。所以我需要检查我是否将 vaules 分配给第一个 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";
            }
        }
     }
4

2 回答 2

3

您可以检查senderGridView1GridView2

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

请注意,仅当GridView1在页面顶部而不是在其中一个 childs 中声明时才有效NamingContainers。然后你可以检查id:

var grid = (GridView)sender;
if( grid.Id == "GridView1" ){}
else{}
于 2012-09-02T19:46:45.793 回答
2

我认为这应该这样做

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     GridView gv = (GridView)sender;
     if(gv.ID == "gv1")
        //do this
     else
        //do that
}
于 2012-09-02T19:54:10.850 回答