Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to filter Gridview using Multi DropdownList
So I used Filter Expresssion

I want to add item to DropList called "Blank " to hide rows which have values = 0 and display only rows which have values (For this column )

and Item Called "ALL" to View All rows (For the column )

(Like Excel Filter )

There is my code

ASP.NET
<asp:DropDownList ID="Ddl_LastBlnc" runat="server" AutoPostBack="True" DataSourceID="LastBlnc" DataTextField="LastBalance" DataValueField="LastBalance" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="" Text="All"></asp:ListItem>

<asp:SqlDataSource ID="LastBlnc" runat="server" ConnectionString="<%$ ConnectionStrings:HHCon %>" 
SelectCommand="SELECT DISTINCT LastBalance From Balance" OnSelecting="LastBlnc_Selecting"></asp:SqlDataSource>


ASP.NET
<asp:DropDownList ID="Ddl_CurntBlnc" runat="server" AutoPostBack="True" DataSourceID="CrntBlnc" DataTextField="CurrentBalance" DataValueField="CurrentBalance" AppendDataBoundItems="True">
 <asp:ListItem Selected="True" Value="" Text="All"></asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="CrntBlnc" runat="server" ConnectionString="<%$ ConnectionStrings:HHCon %>" 
SelectCommand="SELECT DISTINCT CurrentBalance From Balance" OnSelecting="CrntBlnc_Selecting"></asp:SqlDataSource>


// Data source Of GridView 

 <asp:SqlDataSource ID="SqlDataAll" runat="server" ConnectionString="<%$ ConnectionStrings:HHCon %>" 
 SelectCommand="SELECT [Code], [CurrentBalance], [LastBalance] From Balance  " 
 FilterExpression="[LastBalance]={0} AND [CurrentBalance] = {1}  ">
 <FilterParameters>
            <asp:ControlParameter ControlID="Ddl_LastBlnc" Name="LastBalance" 
                    PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="Ddl_CurntBlnc" Name="CurrentBalance" 
                    PropertyName="SelectedValue" Type="String" />
            </FilterParameters>
         </asp:SqlDataSource>


What I have tried:

I tried to add this item Like "All" item in DroplList
But I dont know the filter Expression how it will be !!
Posted
Updated 18-Aug-16 3:34am
v2
Comments
Vincent Maverick Durano 18-Aug-16 9:13am    
I'm not sure if I follow you correctly, are you trying to display all items in the grid if the selected value is "All"?
shms_rony 18-Aug-16 9:20am    
If the dropdown list of the column Selected on "ALL" and the Dropdownlist of the Second Column Selected value is 5 < Gridview display All Rows Of the Column 1 which have values 5 of the column 2

For "Blank " if the dropdownlist of the column selected value is "Blank >> display in Gridview all Data which are not equal 0
Vincent Maverick Durano 18-Aug-16 9:24am    
and these dropdownlist are inside the grid?
shms_rony 18-Aug-16 9:31am    
No I tried this way "inside the header of gridview " But I cant do it :( I tried 3 days but it fails
Then I put Dropdownlist out the gridview in <td>
Vincent Maverick Durano 18-Aug-16 9:35am    
please try the solution I've provided.

1 solution

Have you tried assigning the value of 0 to your "All" option? For example:

ASP.NET
<asp:listitem selected="True" value="0" text="All"></asp:listitem>


When you select "All", the value of 0 will be passed to your FilterExpression along with the value you selected from your second dropdown. So if you select "All" for first dropdown and select "5" for your second dropdown, the filter criteria would result to this:

SQL
"SELECT [Code], [CurrentBalance], [LastBalance] From Balance WHERE [LastBalance]= 0 AND [CurrentBalance] = 5"


If that query returns anything, then it would be reflected to your grid.
 
Share this answer
 
v2
Comments
shms_rony 18-Aug-16 9:38am    
yes I tried this way < But I have record values =0 , So If I did this Grdiview will display thr records which have values =0 and 5
But I dont want to display data with values =0 !!! :/
Vincent Maverick Durano 18-Aug-16 10:07am    
try to change your filter expression to this:
FilterExpression="([LastBalance] <> 0 AND [LastBalance]= {0}) AND [CurrentBalance] = {1}"

EDIT: Forget this. the filter above actually returns nothing when you select "All". The idea is that your "All" value should have a corresponding numeric value in your database so you can easily filter it out.But since you don't have, then you may need to use another sqldatasource with a separate FilterExpression when you opt to select an "All" option from the dropdownlist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900