user1501969
user1501969

Reputation: 127

select row in gridview and display in detailsview

I have a gridview with a select link. Supposedly, when i click on the select link, that particular details to the DataKeyNames should appear.

However, for my case, whenever I click "select" only one record came out and it doesn't change and match to the selected DataKeyNames's data.. I had read through many examples from the net and I doing exactly the same as but it don't work correctly..

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:WholesaleConnectionString %>" 
    SelectCommand="SELECT [poNum], [retailerID] FROM [PO]"></asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="poNum" DataSourceID="SqlDataSource1" >
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="poNum" HeaderText="poNum" InsertVisible="False" 
            ReadOnly="True" SortExpression="poNum" />
        <asp:BoundField DataField="retailerID" HeaderText="retailerID" 
            SortExpression="retailerID" />
    </Columns>
</asp:GridView>


<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:WholesaleConnectionString %>" 
    SelectCommand="SELECT [poNum], [quantity], [unitPrice], [totalAmt], [grandTotal] FROM [PO]">

    <SelectParameters>
    <asp:ControlParameter Name="poNum" ControlID="GridView1" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
   </asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource2" 
    Height="50px" Width="125px">
</asp:DetailsView>

Upvotes: 0

Views: 7688

Answers (1)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

The DetailsView control is also often used in a master-details scenario where the selected record of the master control determines the record to display in the detail view. The following ASP.NET program shows how to display a master-details data from database using GridView and DetailsView control. Here we are using master data as sales data and details as store data. When the user select a row of sales data then it displays the corresponding store details in DetailsView.

Eg:

Default.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br /><b><u>Sales Details</u></b><br /><br />
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
        DataKeyNames="stor_id" AutoGenerateSelectButton="true"  AllowPaging="True" pagesize ="5"  />
        <br /><b><u>Store Details</u></b><br /><br />
        <asp:DetailsView id="DetailsView1" DataSourceID="SqlDataSource2"
        DataKeyNames="stor_id" AllowPaging ="true" Runat="server" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
        SelectCommand="select * from sales" />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
        SelectCommand="select * from stores WHERE stor_id=@stor_id" >
        <SelectParameters>
           <asp:ControlParameter Name="stor_id" ControlID="GridView1" />
        </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Upvotes: 1

Related Questions