Click here to Skip to main content
15,916,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I have a Dropdownlists and a GridView on my WebPage.

1st Dropdown is for month
2nd Dropdown is for Year

In gridview I have a Field with UserName, CreationTime

If i select January from dropdown-1 it must display all the records of January
and also If i select 2012 from dropdownlist 2 ..it must show all the records of January 2012

Please help

This is my dropdowns

XML
<asp:DropDownList ID="ddlMonth" runat="server" Height="20px" Width="130px"
                     AutoPostBack="True" onselectedindexchanged="ddlMonth_SelectedIndexChanged">
                     <asp:ListItem>-- Select Month --</asp:ListItem>
                     <asp:ListItem>January</asp:ListItem>
                     <asp:ListItem>Febrauary</asp:ListItem>
                     <asp:ListItem>March</asp:ListItem>
                     <asp:ListItem>April</asp:ListItem>
                     <asp:ListItem>May</asp:ListItem>
                     <asp:ListItem>June</asp:ListItem>
                     <asp:ListItem>July</asp:ListItem>
                     <asp:ListItem>August</asp:ListItem>
                     <asp:ListItem>September</asp:ListItem>
                     <asp:ListItem>October</asp:ListItem>
                     <asp:ListItem>November</asp:ListItem>
                     <asp:ListItem>December</asp:ListItem>
                 </asp:DropDownList>




XML
<asp:DropDownList ID="ddlYear" runat="server" Height="20px" Width="130px"
                     AutoPostBack="True" onselectedindexchanged="ddlYear_SelectedIndexChanged">
                 <asp:ListItem>-- Select Year --</asp:ListItem>
                     <asp:ListItem>2012</asp:ListItem>
                     <asp:ListItem>2013</asp:ListItem>
                    </asp:DropDownList>



Please help,
by the way am fresher,
thanks in ADVANCE.
Posted
Comments
vyas_pratik20 28-Aug-12 4:15am    
I do not under stand your requirement as you given only aspx code you are doing right only in ddlMonth_SelectedIndexChanged event you first need to check selected index of year if some value is selected you need to call the ddlYear_SelectedIndexChanged directly.
Software Engineer 892 28-Aug-12 4:39am    
Please can you show me how to do this, am unable to understand
Software Engineer 892 28-Aug-12 4:40am    
Just Selecting Month from Dropdownlist must show all the records related to january
Please help

You'll have to filter data supplied by gridView's dataSource.
If you use SqlDataSOurce or ObjectDataSource you supply filtering information to
FilterExpression property to be exact.

I feel lazy to write any code, but read this article, everything is explained here.

If you use EntityDataSource or LInqDataSource then you have to filter data via Where property.
Look here (accepted answer) as a reference.

Good luck.
 
Share this answer
 
You can get selected value from both DropDown and concate strings and using new string fetch records for GridView Display.

VB
Dim strMonth As string 
Dim stryear As string 
Dim strDate As string 

strMonth = ddlMonth.SelectedItem.ToString()
strYear = ddlYear.SelectedItem.ToString()

strDate = strMonth + " " + stryear 


Using this strDate you can fetch results for GridView
 
Share this answer
 
Hello friend you need to something like this...


1) Set AutoPostback property of your both dropdown to True
2) Add SelectedIndexChanged event to your both dropdwon list
3) Make this type of method for binding data to grid and call that method inside
Dropdwon SelectedIndexChange event.
for your reference i made one demo method for binding data to grid.

C#
protected void BindGridData()
    {
        string month, year;
        month = ddlMonth.SelectedValue.ToString();
        year = ddlYear.SelectedValue.ToString();

        string selectQuery = string.Empty;
        if (month != string.Empty && year != string.Empty)
        {
            selectQuery = "SELECT * FROM YOURTABLE WHERE MONTH = " + month + " AND year = "+ year;
        }
        else if (month != string.Empty && year == string.Empty)
        {
            selectQuery = "SELECT * FROM YOURTABLE WHERE MONTH = " + month;
        }
        else if (month == string.Empty && year != string.Empty)
        {
            selectQuery = "SELECT * FROM YOURTABLE WHERE year = "+ year;
        }

        /* write your code to execute select query and get data from database
         * and bind that data to your grid */
    }



this method is just for guide you, you need to write your own method for that as per your requirement.

4) And call that method something like this

C#
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGridData();
}

protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGridData();
}


try this it might help you.
 
Share this answer
 
1. Get all the records in a List List<yourrecords>
2. Create a Funtion FilterGridData(List<yourrecords> oList)
C#
private List<yourrecords> FilterGridData(List<yourrecords> oList)
{
   string selectedMonth = string.empty; 
   string selectedYear = string.empty;
   selectedMonth = ddlMonth.SelectedValue.ToString();
   selectedYear= ddlYear.SelectedValue.ToString();

   if (selectedMonth != string.empty)
   {
      oList= oList.FindAll(p =>p.Month.Trim().IndexOf(selectedMonth ,StringComparison.OrdinalIgnoreCase) >= 0);
   }
if (selectedYear!= string.empty)
   {
      oList = oList.FindAll(p =>p.Year.Trim().IndexOf(selectedYear,StringComparison.OrdinalIgnoreCase) >= 0);
   }
   return oList;
}

3. Call this function on index change event of each dropdown.
 
Share this answer
 
v2

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