有 DataGrid2,但是它是嵌套在 DataGrid1 里的,所以你只能通过 DataGrid1 去找 DataGrid2,而不能直接在 Page 页里找 DataGrid2。在 DataGrid1 的 Item 事件里找:
DataGrid dataGrid2 = (DataGrid)e.Item.FindControl("DataGrid2");
然后用 dataGrid2 去操作。。

解决方案 »

  1.   

    请问在哪里定义:
    DataGrid dataGrid2 = (DataGrid)e.Item.FindControl("DataGrid2");
      

  2.   

    看你要在哪儿用,基本上在datagrid 的消息事件中都可以用这句可以在绑定datagrid1的时候用
    DataGrid dataGrid2 = (DataGrid)(DataGrid1.Items[i].FindControl("DataGrid2"));
      

  3.   

    还是不行,出现的错误:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  4.   

    void myDataGrid_Delete(Object sender,DataGridCommandEventArgs e)
     { 
       string DelCmd="Delete Asia where ID=@ID";
       SqlConnection Conn=new SqlConnection("Server=cao;uid=sa;pwd=;database=sports");
       SqlCommand MyCommand=new SqlCommand(DelCmd,Conn);
       DataGrid dataGrid2=(DataGrid)(DataGrid1.Items[e.Item.ItemIndex].FindControl("DataGrid2"));
       MyCommand.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int,4));
       MyCommand.Parameters["@ID"].Value=dataGrid2.DataKeys[e.Item.ItemIndex];
       MyCommand.Connection.Open();
       MyCommand.ExecuteNonQuery();
       MyCommand.Connection.Close();
       Grid_Bind1();
     }
    在这一句有这样的错误
    MyCommand.Parameters["@ID"].Value=dataGrid2.DataKeys[e.Item.ItemIndex];
    异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名:index
      

  5.   

    当然有问题,你要返回datagrid2的id,你在datagrid2你没有选择行,这句里的e是从datagrid1返回的
      

  6.   

    我这程序搞的乱七八糟的,编辑列点了一下修改都没反应.
    to imfine()帮帮忙.怎么改?
      

  7.   

    datagrid2 的事件响应,响应函数要自己加
    DataGrid myDataGrid=(DataGrid)e.Item.FindControl("DataGrid2");
    myDataGrid.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
      

  8.   

    void Update(Object sender,DataGridCommandEventArgs e)
    {DataGrid dataGrid2=(DataGrid)(DataGrid1.Items[e.Item.ItemIndex].FindControl("DataGrid2"));
     string UpdateCommand;
     UpdateCommand="UPDATE Asia set Data=@Data,Tim=@Tim,Img=@Img,Pl1=@Pl1,Qt1=@Qt1,Pk=@Pk,Qt2=@Qt2,Pl2=@Pl2,O1=@O1,O2=@O2,O3=@O3 where ID=@ID";
     SqlConnection Conn=new SqlConnection("Server=cao;uid=sa;pwd=;database=sports");
     SqlCommand MyCommand=new SqlCommand(UpdateCommand,Conn);
     MyCommand.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int,4));
     MyCommand.Parameters.Add(new SqlParameter("@Data",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Tim",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Img",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Qt1",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Pl1",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Pk",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Qt2",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@Pl2",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@O1",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@O2",SqlDbType.VarChar,50));
     MyCommand.Parameters.Add(new SqlParameter("@O3",SqlDbType.VarChar,50));
      
     MyCommand.Parameters["@ID"].Value=dataGrid2.DataKeys[e.Item.ItemIndex];
     string[] Cols={"@Data","@Tim","@Img","@Pl1","@Qt1","@Pk","@Qt2","@Pl2","@O1","@O2","@O3"};
     int NumCols=e.Item.Cells.Count;
     int i;
     for(i=2;i<NumCols-1;i++)
     {
      TextBox CurrentTextBox;
      CurrentTextBox=(TextBox)e.Item.Cells[i].Controls[0];
      string ColValue=CurrentTextBox.Text; 
      MyCommand.Parameters[Cols[i-2]].Value=ColValue;
      }
      MyCommand.Connection.Open();
      MyCommand.ExecuteNonQuery();
      MyCommand.Connection.Close();
      dataGrid2.EditItemIndex=-1;
      Grid_Bind1();
      }
      

  9.   

    你总是把datagrid1的e用到datagrid2,这个update是接受的datagrid1的事件吧,datagrid1和datagrid2的事件必须分开写