开发者

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

开发者 https://www.devze.com 2023-03-28 16:56 出处:网络
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 gridVie

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

精彩评论

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

关注公众号