This is a example of populating three different DetailsView based on selection of of record in a GridView using Multiple DataKeyNames in C sharp and ASP .NET
In this example GridView is populated from a table called Website using SqlDataSource, on GridVies i have defined multiple (3) DataKeyNames separated by comma, which will be used to fetch the record related to those DataKeyName from 3 tables in 3 DetailsViews
Download source code for Multiple DetailsView using DataKeyNames of a GridView
The html code for this is would be
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
DataKeyNames="Record,ResponseID,Source_id"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
OnSelectedIndexChanged=
"GridView1_SelectedIndexChanged2">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Record"
HeaderText="Record"
SortExpression="Record" />
<asp:BoundField DataField="ResponseID"
HeaderText="ResponseID"
SortExpression="ResponseID" />
<asp:BoundField DataField="Source_id"
HeaderText="Source_id"
SortExpression="Source_id" />
<asp:BoundField DataField="Remarks"
HeaderText="Remarks"
SortExpression="Remarks" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand=
"Select Record, App_id as ResponseID,
Source_id,Remarks from Website">
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView1"
runat="server"
DataKeyNames="Record"
AutoGenerateRows="False"
DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Record"
HeaderText="Record"
SortExpression="Record" />
<asp:BoundField DataField="Name"
HeaderText="Name"
SortExpression="Name" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2"
runat="server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand=
"SELECT [Record], [Name]
FROM [Applications]
WHERE ([Record] = @Record)">
<SelectParameters>
<asp:ControlParameter
ControlID="GridView1"
Name="Record"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView2"
runat="server" DataKeyNames="App_id"
AutoGenerateRows="False"
DataSourceID="SqlDataSource3"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="App_id"
HeaderText="App_id"
SortExpression="App_id" />
<asp:BoundField DataField="Details"
HeaderText="Details"
SortExpression="Details" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource3"
runat="server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand=
"SELECT [App_id], [Details]
FROM [Response]
WHERE ([App_id] = @ResponseID)">
<SelectParameters>
<asp:ControlParameter
ControlID="GridView1"
Name="ResponseID"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView3"
runat="server" DataKeyNames="Source_id"
AutoGenerateRows="False"
DataSourceID="SqlDataSource4"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Source_ID"
HeaderText="Source_ID"
SortExpression="Source_ID" />
<asp:BoundField DataField="LastName"
HeaderText="LastName"
SortExpression="LastName" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource4"
runat="server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand=
"SELECT [Source_ID], [LastName]
FROM [advt]
WHERE ([Source_ID] = @Source_ID)">
<SelectParameters>
<asp:ControlParameter
ControlID="GridView1"
Name="Source_ID"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
</div>
</form>
</body>
</html>
And the Code Behind for this goes like this
protected void GridView1_SelectedIndexChanged2(object sender, EventArgs e)
string App_id = GridView1.DataKeys[GridView1.SelectedIndex]["ResponseID"].ToString();
SqlDataSource3.SelectParameters.Clear();
SqlDataSource3.SelectParameters.Add("ResponseID", App_id);
DetailsView2.DataBind();
string Source_id = GridView1.DataKeys[GridView1.SelectedIndex]["Source_id"].ToString();
SqlDataSource4.SelectParameters.Clear();
SqlDataSource4.SelectParameters.Add("Source_id", Source_id);
DetailsView3.DataBind();

About the Author