paddylast

paddylast

  • NA
  • 12
  • 0

Datagrid selection

Oct 10 2004 5:18 PM
I'm using an edit, update and cancel button for each row in my datagrid. What I would like to do is store the value of the Ranking field of the record that is selected before it is changed. In my update method the value is already changed so I guess I should store it in the the Edit method, but I am not sure what code to use to get at it. Here is the code I currently use: private void dgLadder_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { dgLadder.EditItemIndex = (int)e.Item.ItemIndex; BindData(); } private void dgLadder_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { String updateCmd = "UPDATE Ladder SET LastName = @NLName, FirstName = @NFName, Ranking = @NRanking where Id = @SelectedId"; SqlCommand myCommand = new SqlCommand(updateCmd, connLadder); myCommand.Parameters.Add(new SqlParameter("@SelectedId",SqlDbType.NVarChar, 10)); myCommand.Parameters.Add(new SqlParameter("@NLName",SqlDbType.NVarChar, 10)); myCommand.Parameters.Add(new SqlParameter("@NFName",SqlDbType.NVarChar, 10)); myCommand.Parameters.Add(new SqlParameter("@NRanking",SqlDbType.NVarChar, 10)); myCommand.Parameters["@SelectedId"].Value = dgLadder.DataKeys[(int)e.Item.ItemIndex]; String nLName = ((TextBox)e.Item.Cells[0].Controls[0]).Text; String nFName = ((TextBox)e.Item.Cells[1].Controls[0]).Text; String nRanking = ((TextBox)e.Item.Cells[2].Controls[0]).Text; myCommand.Parameters["@NLName"].Value = nLName; myCommand.Parameters["@NFName"].Value = nFName; myCommand.Parameters["@NRanking"].Value = nRanking; myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); dgLadder.EditItemIndex = -1; myCommand.Connection.Close(); BindData(); } I've tried using the following in the edit method: String oldRanking = ((TextBox)e.Item.Cells[2].Controls[0]).Text; but get error Specified argument was out of the range of valid values. Parameter name: index Can anyone help on this? Thanks