views:

1270

answers:

2

Hello.

For my view, In the page: 1) I have a gridview with the select hyperlink in it. The gridview data is from the SQLDataSource. 2) And, I also have a few textboxes (abt 5) - not in the gridview.

What I would like to do is to use the select hyperlink to select the row that i want to edit. And when i click select, the data in the row should come out to their respective textboxes. How do I go about doing this?

A: 

Grid views have inline record editing support, you can enable this functionality by setting the AutoGenerateEditButton property to true. You must specify the name of a stored procedure or SQL query in the UpdateCommand property, this is used to update data in the underlying data base.

From MSDN:

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>

      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

See here for the full example code.

Phaedrus
A: 

Wireup OnSelectedIndexChanged event:

ASPX:

<asp:GridView id="gvTest"  OnSelectedIndexChanged="gvTest_SelectedIndexChanged"
 ..........></asp:GridView>

<asp:TextBox id="text1" runat="server"/>

Code:

 protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
 {
    //get currently selected row
    var r =gvTest.Rows[gvTest.SelectedIndex];

    //THIS WAY YOU CAN GET TEXT FROM ALL COLUMNS
    text1.Text = r.Cells[r.Cells.Count - 1].Text;
 }
TheVillageIdiot