3

I have a formview and a nested gridview where i would like to be able to select the value of a specific cell when the row has been updated. I took the example code from the msdn site as it's close to what i want:

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

       GridView SprayGrid = (GridView)FormView1.FindControl("GridView1");

       int Index = SprayGrid.EditIndex;
       GridViewRow row = SprayGrid.Rows[Index];


       TextBox message = (TextBox)FormView1.FindControl("TextBox1");
         message.Text = row.Cells[4].Text;
    }

However, for some reason this will only pick up the index of the cell, i.e. if i change the row.cells[0] i can see in my message box the index number if i want to see any other cell then the message is blank? Any ideas would be great.

VinayC
  • 47,395
  • 5
  • 59
  • 72
Will
  • 31
  • 2
  • this can happen if your grid has not bound to data as of yet. You can check if grid-view's RowDataBound event is getting fired before RowUpdated event. Work-around can be explicitly binding the grid every time postback happens instead of relying on view-state. – VinayC Aug 16 '11 at 12:15
  • Vinay, i think you could be on the right track. How can I check if grid-view's RowDataBound event is getting fired before RowUpdated event? And do you have an example of how I explicitly bind the grid? Thanks again for all your help this has been a real headache for me!! – Will – Will Aug 17 '11 at 08:20
  • use VS debugger and put break-point in both events - that should tell you if grid is getting bound or not. Binding grid explicitly is nothing but setting its data-source property to actual data and invoking `databind` method. You should do this early in page-life cycle (page_load should be good enough). – VinayC Aug 17 '11 at 09:53
  • ok i see what you mean but i dont think this is it then. Im fairly sure the grid is correctly databound. I have set the datasource to only return records based on the value of a control on the formview that it's nested in and this works fine including any updates i want to do. Problem is jsut that i cant return the value from the cells :@ – Will Aug 17 '11 at 10:12

1 Answers1

1

Use DataKeys on the GridView, and instead of trying to set the TextBox value from the cell, set it from the DataKey.

In the ASPX:

<asp:GridView Id="GridView1" runat="server" DataKeyNames="SomeColumnName">

And in your row updating event:

TextBox message = (TextBox)FormView1.FindControl("TextBox1");
message.Text = SprayGrid.DataKeys[row.RowIndex]["SomeColumnName"].ToString();
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Thanks for the quick response James, I'm already using a key name for the key field on my grid. Sorry for not being clear but i actually just want to retrieve the text value of it, im putting it in a text box just as a test for me to check it's been returned. – Will Aug 16 '11 at 19:22
  • Vinay, i think you could be on the right track. How can I check if grid-view's RowDataBound event is getting fired before RowUpdated event? And do you have an example of how I explicitly bind the grid? Thanks again for all your help this has been a real headache for me!! – Will Aug 16 '11 at 19:28
  • James, I got this bit working thanks! I realised you could have multiple datakeynames so that's what I done. However my update no longer works now on the grid. If i select the row for editing and update it, the value goes back to what ever the previous value was, could this be what Vinay was refering to? – Will Aug 17 '11 at 08:50