locked
want to bind a gridview using the another gridview's selected item RRS feed

  • Question

  • User-1313886874 posted

     iam using a gridview1 which holds the feilds like (Cust_ID, date, ......). Now what i want to do is, when i click on any Cust_ID in the gridview1, it should bind the another gridview2 using the Cust_ID value, in the same page itself....

    Friday, October 3, 2008 7:30 AM

Answers

  • User-1995538749 posted

    Here's a very simple example: 

    <%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    	codefile="1328980.aspx.cs" inherits="Posts_GeneralASPNET_DataPresentationControls_1328980"
    	title="Untitled Page" %>
    
    <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    	<asp:gridview id="GridView1" runat="server" allowpaging="True" autogeneratecolumns="False"
    		autogenerateselectbutton="true" datakeynames="ProductID" datasourceid="SqlDataSource1">
    		<columns>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    			<asp:boundfield datafield="CategoryName" headertext="CategoryName" sortexpression="CategoryName" />
    		</columns>
    	</asp:gridview>
    	<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    		selectcommand="SELECT [ProductID], [ProductName], [CategoryName] FROM [Alphabetical list of products]">
    	</asp:sqldatasource>
    	<asp:gridview id="GridView2" runat="server" autogeneratecolumns="False" datakeynames="ProductID"
    		datasourceid="SqlDataSource2" style="margin-top: 20px;">
    		<columns>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    			<asp:boundfield datafield="SupplierID" headertext="SupplierID" sortexpression="SupplierID" />
    			<asp:boundfield datafield="CategoryID" headertext="CategoryID" sortexpression="CategoryID" />
    			<asp:boundfield datafield="QuantityPerUnit" headertext="QuantityPerUnit" sortexpression="QuantityPerUnit" />
    			<asp:boundfield datafield="UnitPrice" headertext="UnitPrice" sortexpression="UnitPrice" />
    			<asp:boundfield datafield="UnitsInStock" headertext="UnitsInStock" sortexpression="UnitsInStock" />
    			<asp:boundfield datafield="UnitsOnOrder" headertext="UnitsOnOrder" sortexpression="UnitsOnOrder" />
    			<asp:boundfield datafield="ReorderLevel" headertext="ReorderLevel" sortexpression="ReorderLevel" />
    			<asp:checkboxfield datafield="Discontinued" headertext="Discontinued" sortexpression="Discontinued" />
    			<asp:boundfield datafield="CategoryName" headertext="CategoryName" sortexpression="CategoryName" />
    		</columns>
    	</asp:gridview>
    	<asp:sqldatasource id="SqlDataSource2" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    		selectcommand="SELECT * FROM [Alphabetical list of products] WHERE ([ProductID] = @ProductID)">
    		<selectparameters>
    			<asp:controlparameter controlid="GridView1" name="ProductID" propertyname="SelectedValue"
    				type="Int32" />
    		</selectparameters>
    	</asp:sqldatasource>
    </asp:content>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 3, 2008 8:22 AM
  • User-1995538749 posted

    Then simply use a TemplateField with an embedded LinkButton like so for your Select command: 

    <asp:templatefield>
    	<itemtemplate>
    		<asp:linkbutton id="btnSelect" runat="server" commandname="Select" text='<%# Eval("ProductID") %>' />
    	</itemtemplate>
    </asp:templatefield>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 6, 2008 8:06 AM

All replies

  • User-1995538749 posted

    Here's a very simple example: 

    <%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    	codefile="1328980.aspx.cs" inherits="Posts_GeneralASPNET_DataPresentationControls_1328980"
    	title="Untitled Page" %>
    
    <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    	<asp:gridview id="GridView1" runat="server" allowpaging="True" autogeneratecolumns="False"
    		autogenerateselectbutton="true" datakeynames="ProductID" datasourceid="SqlDataSource1">
    		<columns>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    			<asp:boundfield datafield="CategoryName" headertext="CategoryName" sortexpression="CategoryName" />
    		</columns>
    	</asp:gridview>
    	<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    		selectcommand="SELECT [ProductID], [ProductName], [CategoryName] FROM [Alphabetical list of products]">
    	</asp:sqldatasource>
    	<asp:gridview id="GridView2" runat="server" autogeneratecolumns="False" datakeynames="ProductID"
    		datasourceid="SqlDataSource2" style="margin-top: 20px;">
    		<columns>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    			<asp:boundfield datafield="SupplierID" headertext="SupplierID" sortexpression="SupplierID" />
    			<asp:boundfield datafield="CategoryID" headertext="CategoryID" sortexpression="CategoryID" />
    			<asp:boundfield datafield="QuantityPerUnit" headertext="QuantityPerUnit" sortexpression="QuantityPerUnit" />
    			<asp:boundfield datafield="UnitPrice" headertext="UnitPrice" sortexpression="UnitPrice" />
    			<asp:boundfield datafield="UnitsInStock" headertext="UnitsInStock" sortexpression="UnitsInStock" />
    			<asp:boundfield datafield="UnitsOnOrder" headertext="UnitsOnOrder" sortexpression="UnitsOnOrder" />
    			<asp:boundfield datafield="ReorderLevel" headertext="ReorderLevel" sortexpression="ReorderLevel" />
    			<asp:checkboxfield datafield="Discontinued" headertext="Discontinued" sortexpression="Discontinued" />
    			<asp:boundfield datafield="CategoryName" headertext="CategoryName" sortexpression="CategoryName" />
    		</columns>
    	</asp:gridview>
    	<asp:sqldatasource id="SqlDataSource2" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    		selectcommand="SELECT * FROM [Alphabetical list of products] WHERE ([ProductID] = @ProductID)">
    		<selectparameters>
    			<asp:controlparameter controlid="GridView1" name="ProductID" propertyname="SelectedValue"
    				type="Int32" />
    		</selectparameters>
    	</asp:sqldatasource>
    </asp:content>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 3, 2008 8:22 AM
  • User-1313886874 posted

     hi ecbruck.........

         thanx for ur reply....   still i have one problem, i use ur code, but i cant get link with the productID. it display the select in a seperate column. but i want my productID column as the selecting feild. i want to click the productID to bind the another grid, and not by the seperate select feild...

          what should i do for that??

    Monday, October 6, 2008 3:46 AM
  • User-1995538749 posted

    Then simply use a TemplateField with an embedded LinkButton like so for your Select command: 

    <asp:templatefield>
    	<itemtemplate>
    		<asp:linkbutton id="btnSelect" runat="server" commandname="Select" text='<%# Eval("ProductID") %>' />
    	</itemtemplate>
    </asp:templatefield>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 6, 2008 8:06 AM
  • User-1313886874 posted

    [:D]  Thank u very much ecbruck....... i got it.....    really superbv!!!!!      thanks alot........................

              

    Monday, October 6, 2008 8:30 AM