ASP.NET GridView DetailsView Master Detail Example: Display Selected GridView Row in DetailsView in ASP.NET

HTML Markup

The HTML Markup consists of an ASP.Net GridView with two BoundField columns, one hidden TemplateField column with a Label and one ButtonField column which consists of a Select Button to select the GridView Row. Below the GridView, there’s a DetailsView control for displaying the selected GridView row details.
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">  
  2.     <Columns>  
  3.         <asp:BoundField DataField="Id" HeaderText="Id" />  
  4.         <asp:BoundField DataField="Name" HeaderText="Name" />  
  5.         <asp:TemplateField HeaderText="Country" Visible="false">  
  6.             <ItemTemplate>  
  7.                 <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>  
  8.             </ItemTemplate>  
  9.         </asp:TemplateField>  
  10.         <asp:ButtonField Text="Select" CommandName="Select" />  
  11.     </Columns>  
  12. </asp:GridView>  
  13. <br />  
  14. <u>Selected Row:</u>  
  15. <br />  
  16. <br />  
  17. <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false">  
  18.     <Fields>  
  19.         <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-Font-Bold="true" />  
  20.         <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Font-Bold="true" />  
  21.         <asp:BoundField DataField="Description" HeaderText="Description" HeaderStyle-Font-Bold="true" />  
  22.     </Fields>  
  23. </asp:DetailsView>  
C#
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {  
  3.     if (!this.IsPostBack)   
  4.     {  
  5.         DataTable dt = new DataTable();  
  6.         dt.Columns.AddRange(new DataColumn[3]   
  7.         {  
  8.             new DataColumn("Id"typeof(int)),  
  9.                 new DataColumn("Name"typeof(string)),  
  10.                 new DataColumn("Description"typeof(string))  
  11.         });  
  12.         dt.Rows.Add(1, "John Hammond""Works as a scientist in USA.");  
  13.         dt.Rows.Add(2, "Mudassar Khan""ASP.Net programmer and consultant in India.");  
  14.         dt.Rows.Add(3, "Suzanne Mathews""Content Writer in France.");  
  15.         dt.Rows.Add(4, "Robert Schidner""Wild life photographer in Russia.");  
  16.         GridView1.DataSource = dt;  
  17.         GridView1.DataBind();  
  18.     }  
  19. }  
Displaying GridView Selected Row in DetailsView control

When the Select Button is clicked, the OnSelectedIndexChanged event handler of the ASP.Net GridView is triggered. The ID and Name values are extracted directly from the BoundField column, while the Description value is extracted via finding the Label control inside the TemplateField column.

The fetched values are inserted into a dynamic DataTable which is finally used to populate the DetailsView control.

C#
  1. protected void OnSelectedIndexChanged(object sender, EventArgs e)  
  2. {  
  3.     string id = GridView1.SelectedRow.Cells[0].Text;  
  4.     string name = GridView1.SelectedRow.Cells[1].Text;  
  5.     string description = (GridView1.SelectedRow.FindControl("lblDescription"as Label).Text;  
  6.     DataTable dt = new DataTable();  
  7.     dt.Columns.AddRange(new DataColumn[3]  
  8.     {  
  9.         new DataColumn("Id"typeof(int)),  
  10.             new DataColumn("Name"typeof(string)),  
  11.             new DataColumn("Description"typeof(string))  
  12.     });  
  13.     dt.Rows.Add(id, name, description);  
  14.     DetailsView1.DataSource = dt;  
  15.     DetailsView1.DataBind();  
  16. }