简体   繁体   中英

how to get parameters from a specific field of row in gridView for a templatefield at the same row

I'm using GridView to display data from dataBase, the gridView is bound to objectDataSource. each row in gridview represent an item that belongs to user and I want to add another field to the gridView that will display the users detail (that actually are in a different table at the dataBase).

I'm not sure what is the best way to do that, I tried to add to the gridView a TemplateField that contain a DetailView, and bound it to another objectdatasource but I don't know how to take a parameter from a specific field at the row-(the userID field).

Any suggestions would be welcome...

It's going to be a little trickier than that. You can set the userID as a data key, and in the RowDataBound event of the GridView, bind the detail view.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="UserID" OnRowDataBound="GridView1_RowDataBound">

And in the code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //get the data key
    int userID = (int)GridView1.DataKeys[e.Row.RowIndex]["UserID"];

    //get the nested details view control
    DetailsView dv = (DetailsView)e.Row.FindControl("DetailsView1");

    dv.DataSource = GetUserDetailsTable(userID); //your data source
    dv.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM