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();
}
精彩评论