locked
Want my dropdownlist to populate only data from the selection to the GridView.. RRS feed

  • Question

  • User1952956959 posted

    Hello,

    I've got a dropdownlist control and a GridView control. I think I'm close but I need a little help.

    When I select an Item from the dropdownlist control, I would like for the GridView to select only data from the "Name" that is selected.

    Instead, I get everything in the table.

    Any ideas?

    My code...

    <asp:dropdownlist id="ddlClients" runat="server" appenddatabounditems="True" autopostback="True"
    datasourceid="sdsClients" datatextfield="Name" datavaluefield="ID">
    		<asp:listitem text="Select a Client" value="-1">
    		</asp:listitem>
    	</asp:dropdownlist>
    	<asp:sqldatasource id="sdsClients" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" selectcommand="SELECT * FROM [Current]">
    	</asp:sqldatasource>
    	<asp:sqldatasource id="Sqldatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
    selectcommand="SELECT * FROM [Current] WHERE ([ID] = CASE WHEN @ID = -1 THEN [ID] ELSE @ID END) ORDER BY [Name]">
    		<selectparameters>
    			<asp:controlparameter controlid="ddlClients" name="ID" propertyname="SelectedValue"
    type="Int32" />
    		</selectparameters>
    	</asp:sqldatasource>
        <strong><p>Client Details</p>
    	<asp:gridview id="gvClient_Details" runat="server" autogeneratecolumns="False" datakeynames="ID"
    datasourceid="sdsClients" style="margin-top: 12px;" Width="783px">
    		<columns>
    			<asp:boundfield datafield="Name" headertext="Name" sortexpression="Name" />
    			<asp:boundfield datafield="Location" headertext="Location" sortexpression="Location" />
    <asp:boundfield datafield="Street" headertext="Street" sortexpression="Street" />
    <asp:boundfield datafield="System" headertext="System" sortexpression="System" />
    <asp:boundfield datafield="Payment" headertext="Payment" sortexpression="Payment" />
    <asp:boundfield datafield="Server" headertext="Server" sortexpression="Server" />
    <asp:boundfield datafield="Phone" headertext="Phone" sortexpression="Phone" />
    <asp:boundfield datafield="Circuit_Provider" headertext="Circuit Provider" sortexpression="Circuit_Provider" />
    <asp:boundfield datafield="Location" headertext="Location" sortexpression="Location" />
    <asp:boundfield datafield="Circuit_ID" headertext="Circuit ID" sortexpression="Circuit_ID" />
    <asp:boundfield datafield="Location" headertext="Location" sortexpression="Location" />
    <asp:boundfield datafield="Server_Login" headertext="Server Login" sortexpression="Server_Login" />
    
    		</columns>
    	</asp:gridview>

    Wednesday, June 5, 2013 9:19 PM

Answers

  • User1938476581 posted

    Hi,

    The code works for me.

    <asp:DropDownList ID="DropDownList1" runat="server" appenddatabounditems="True" DataSourceID="SqlDataSource1" datatextfield="userName" datavaluefield="ID" AutoPostBack="true">
           <asp:listitem text="Select a Client" value="-1">
    		</asp:listitem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MydatabaseConnectionString %>" ProviderName="<%$ ConnectionStrings:MydatabaseConnectionString.ProviderName %>"  SelectCommand="SELECT * FROM [userInfo]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1"  DataSourceID="SqlDataSource2" DataKeyNames="ID"  runat="server" AutoGenerateColumns="False">
            <Columns>          
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="userName" HeaderText="userName" SortExpression="userName" />
                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
                <asp:BoundField DataField="friend" HeaderText="friend" SortExpression="friend" />           
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MydatabaseConnectionString %>" ProviderName="<%$ ConnectionStrings:MydatabaseConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [userInfo] where ([ID]= CASE WHEN @ID = -1 THEN [ID] ELSE @ID END) order by [userName]">
            <selectparameters>
    			<asp:controlparameter controlid="DropDownList1" name="ID" propertyname="SelectedValue" type="Int32" />
    		</selectparameters>
        </asp:SqlDataSource>

    Please check it. If it still not work for you. You can debug it and find the real issue.

    Hope it can help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2013 1:05 AM

All replies

  • User-1716253493 posted
    selectcommand="SELECT * FROM [Current] WHERE ([ID] = @ID) OR (@ID = -1) ORDER BY [Name]"
    Wednesday, June 5, 2013 10:24 PM
  • User1952956959 posted

    Hi oned_gk,

    I tried this method. Still getting the whole table in the GridView. I changde both selectcommand's to this.

    Hmm..

    Wednesday, June 5, 2013 10:40 PM
  • User1938476581 posted

    Hi,

    The code works for me.

    <asp:DropDownList ID="DropDownList1" runat="server" appenddatabounditems="True" DataSourceID="SqlDataSource1" datatextfield="userName" datavaluefield="ID" AutoPostBack="true">
           <asp:listitem text="Select a Client" value="-1">
    		</asp:listitem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MydatabaseConnectionString %>" ProviderName="<%$ ConnectionStrings:MydatabaseConnectionString.ProviderName %>"  SelectCommand="SELECT * FROM [userInfo]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1"  DataSourceID="SqlDataSource2" DataKeyNames="ID"  runat="server" AutoGenerateColumns="False">
            <Columns>          
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="userName" HeaderText="userName" SortExpression="userName" />
                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
                <asp:BoundField DataField="friend" HeaderText="friend" SortExpression="friend" />           
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MydatabaseConnectionString %>" ProviderName="<%$ ConnectionStrings:MydatabaseConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [userInfo] where ([ID]= CASE WHEN @ID = -1 THEN [ID] ELSE @ID END) order by [userName]">
            <selectparameters>
    			<asp:controlparameter controlid="DropDownList1" name="ID" propertyname="SelectedValue" type="Int32" />
    		</selectparameters>
        </asp:SqlDataSource>

    Please check it. If it still not work for you. You can debug it and find the real issue.

    Hope it can help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2013 1:05 AM
  • User1952956959 posted

    I got it working.

    I put the ddlist along side a search bar were data can be filtered both ways.

    Thanks everyone.

    Friday, June 7, 2013 1:48 AM