Populate DetailsView From GridView Select DataKeyNames

Here i'm explaining how to Populate DetailsView From GridView In Asp.Net based on selection of a record in GridView using Multiple DataKeyNames

Populate multiple detailsview from gridview select
In this example GridView is populated from a table called Website using SqlDataSource.

I have defined multiple(3) comma separated DataKeyNames Property in GridView source.

These DataKeyNames properties will be used to fetch the record related to respective DataKey from 3 tables to populate 3 DetailsViews.

HTML SOURCE OF THE PAGE
   1:  <form id="form1" runat="server">
   2:  <div>
   3:  <asp:GridView ID="GridView1" runat="server" 
   4:                DataKeyNames="Record,ResponseID,Source_id"  
   5:                AutoGenerateColumns="False" 
   6:                DataSourceID="SqlDataSource1" 
   7:                OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
   8:  <Columns>
   9:  <asp:CommandField ShowSelectButton="True" />
  10:  <asp:BoundField DataField="Record" HeaderText="Record"/>
  11:  <asp:BoundField DataField="ResponseID" HeaderText="ResponseID"/>
  12:  <asp:BoundField DataField="Source_id" HeaderText="Source_id"/>
  13:  <asp:BoundField DataField="Remarks" HeaderText="Remarks"/>
  14:  </Columns>
  15:  </asp:GridView>
  16:   
  17:  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  18:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  19:  SelectCommand="Select Record, App_id as ResponseID, 
  20:                 Source_id,Remarks from Website">
  21:  </asp:SqlDataSource>
  22:   
  23:  <table><tr><td>
  24:  <asp:DetailsView ID="DetailsView1" runat="server" 
  25:                   DataKeyNames="Record" AutoGenerateRows="False"
  26:                   DataSourceID="SqlDataSource2">
  27:  <Fields>
  28:  <asp:BoundField DataField="Record" HeaderText="Record"/>
  29:  <asp:BoundField DataField="Name" HeaderText="Name"/>
  30:  </Fields>
  31:  </asp:DetailsView>
  32:   
  33:  <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
  34:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  35:  SelectCommand="SELECT [Record], [Name] FROM 
  36:                 [Applications] WHERE ([Record] = @Record)">
  37:  <SelectParameters>
  38:  <asp:ControlParameter ControlID="GridView1" Name="Record" 
  39:                        PropertyName="SelectedValue"
  40:                        Type="String" />
  41:  </SelectParameters>
  42:  </asp:SqlDataSource></td>
  43:   
  44:  <td>
  45:  <asp:DetailsView ID="DetailsView2" runat="server" 
  46:                   DataKeyNames="App_id" AutoGenerateRows="False" 
  47:                   DataSourceID="SqlDataSource3">
  48:  <Fields>
  49:  <asp:BoundField DataField="App_id" HeaderText="App_id"/>
  50:  <asp:BoundField DataField="Details" HeaderText="Details"/>
  51:  </Fields>
  52:  </asp:DetailsView>
  53:   
  54:  <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
  55:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  56:  SelectCommand="SELECT [App_id], [Details] FROM [Response] 
  57:                 WHERE ([App_id] = @ResponseID)">
  58:  <SelectParameters>
  59:  <asp:ControlParameter ControlID="GridView1" 
  60:                        Name="ResponseID" 
  61:                        PropertyName="SelectedValue"
  62:                        Type="String" />
  63:  </SelectParameters>
  64:  </asp:SqlDataSource></td>
  65:          
  66:  <td>
  67:  <asp:DetailsView ID="DetailsView3" runat="server" 
  68:                   DataKeyNames="Source_id" 
  69:                   AutoGenerateRows="False" 
  70:                   DataSourceID="SqlDataSource4">
  71:  <Fields>
  72:  <asp:BoundField DataField="Source_ID" HeaderText="Source_ID"/>
  73:  <asp:BoundField DataField="LastName" HeaderText="LastName"/>
  74:  </Fields>
  75:  </asp:DetailsView>
  76:   
  77:  <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
  78:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  79:  SelectCommand="SELECT [Source_ID], [LastName] FROM [advt] 
  80:                 WHERE ([Source_ID] = @Source_ID)">
  81:  <SelectParameters>
  82:  <asp:ControlParameter ControlID="GridView1" 
  83:                        Name="Source_ID" 
  84:                        PropertyName="SelectedValue"
  85:                        Type="String" />
  86:  </SelectParameters>
  87:  </asp:SqlDataSource></td>
  88:  </tr></table>
  89:  </div>
  90:  </form>

Write below mentioned code in SelectedIndexChanged Event of Gridveiw.

C# CODE
protected void GridView1_SelectedIndexChanged(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();
    }


VB.NET CODE
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs)
 Dim App_id As String = GridView1.DataKeys(GridView1.SelectedIndex)("ResponseID").ToString()
 SqlDataSource3.SelectParameters.Clear()
 SqlDataSource3.SelectParameters.Add("ResponseID", App_id)
 DetailsView2.DataBind()

 Dim Source_id As String = GridView1.DataKeys(GridView1.SelectedIndex)("Source_id").ToString()
 SqlDataSource4.SelectParameters.Clear()
 SqlDataSource4.SelectParameters.Add("Source_id", Source_id)
 DetailsView3.DataBind()
End Sub

Build and run the code.

Download Sample Code



13 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,

    Just too beautiful!

    But my problem is how do I create the code if i want to open the detailsviews in another separate pages using the click event of a buttonfield template in the gridview.

    Expecting your reply; thank you.

    Emmao

    ReplyDelete
  4. @Emmao :

    I have written a article which exactly fits to youe need

    read Post mentioned below

    Link button in GridView and querystring to pass data on other page


    Do let me know if it solves your query or not ?

    ReplyDelete
  5. Very nice solution
    keep it up!
    Thanks

    ReplyDelete
  6. Hi Amit ,
    I have some problem in my project is that in my form i have created dropdown list and in my dropdown list two listitem is there one is ID and other is Name when i select ID and type in textbox ETH001 i.e ID and click on search button then only one record should be shown in a gridview from all records of a gridview.
    pls help me.
    Thanks in advance

    ReplyDelete
  7. thanks for the info...
    have a great day...

    ReplyDelete
  8. Hi...
    Please provide correct downloadable link for this. I'm not able to download the code.

    ReplyDelete