有 DataGrid2,但是它是嵌套在 DataGrid1 里的,所以你只能通过 DataGrid1 去找 DataGrid2,而不能直接在 Page 页里找 DataGrid2。在 DataGrid1 的 Item 事件里找:
DataGrid dataGrid2 = (DataGrid)e.Item.FindControl("DataGrid2");
然后用 dataGrid2 去操作。。
DataGrid dataGrid2 = (DataGrid)e.Item.FindControl("DataGrid2");
然后用 dataGrid2 去操作。。
DataGrid dataGrid2 = (DataGrid)e.Item.FindControl("DataGrid2");
DataGrid dataGrid2 = (DataGrid)(DataGrid1.Items[i].FindControl("DataGrid2"));
{
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
to imfine()帮帮忙.怎么改?
DataGrid myDataGrid=(DataGrid)e.Item.FindControl("DataGrid2");
myDataGrid.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
{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();
}