Hi..

I have 3 DropDownList:-

DropdownList1 has status,DropDownlist2 has date and DropDownList3 has duedate and based on these values selected i want to populate the Gridview following is my code..

 protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlDataAdapter sda = new SqlDataAdapter("Select * from DropDownFilter where status='" + DropDownList1.SelectedValue + "' and date='" + DropDownList2.SelectedValue + "' and duedate='" + DropDownList3.SelectedValue + "'", con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

But unable to achieve it,can anyone please guide me??

what is the error message?

Gridview is not getting populated..

Well I couldn't see in your code the "Connection object". And connection open method. Please see the following simple example:

http://www.dotnetperls.com/sqldataadapter

Second thing, store the dynamic query into variable and run it in SQL management studio to check either query is good.

Check the result in dataset, either it is filled or not.

Else grid binding is correct.

Hi..

I can filter gridview by only one dropdownlist not by other two dropdowns..

Couldn't get you, you wrote the code under button click event and in event's body you are getting the values from dropdowns then how it is possible to get data against one dropdown but not others? If you want to handle it on drop down selected index change event then you have to turn on the AutoPostback property of all dropdowns and write all code in SelectedIndexChange event. Looks fairly simple, don't know howz your development enviorment is setup.

<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" >
<asp:ListItem>open</asp:ListItem>
<asp:ListItem>close</asp:ListItem>
<asp:ListItem>pending</asp:ListItem>

</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource2" DataTextField="date" DataValueField="id"
AutoPostBack="True"
>
</asp:DropDownList>
&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource3" DataTextField="duedate" DataValueField="id"
AutoPostBack="True"
>
</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
<br />
<br />

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource4">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="companyname" HeaderText="companyname"
SortExpression="companyname" />
<asp:BoundField DataField="status" HeaderText="status"
SortExpression="status" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:BoundField DataField="duedate" HeaderText="duedate"
SortExpression="duedate" />
</Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT id, companyname, status, date, duedate FROM DropDownFilter">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="status"
Name="status" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" DefaultValue="date" Name="date"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList3" DefaultValue="" Name="duedate"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

This is what i am trying but unable to filter gridview by dropdownlist..

Please also send the C# cs file code, wants to know how you handled this.

I am trying to filter it here itself using filter parameters..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.