开发者

Cant get value of cell on gridview nested in formview

I have a formview and a nested gridview where i would like to be able to select th开发者_开发问答e 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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜