Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETGridviewSorting, +
Hello FRNDS, Please solve my problem facing with GRIDVIEW AND DROPDOWNLIST.
 
Am working on ASp.NET, c#, SqlSERVER2005.
 
On my webpage I have a Dropdownlist and a Gridview...data is dispalying perfectly from database. and this gridview contains PAGING ALSO(it contains Large number of records in GRIDVIEW) and the thing here is I need to sort the data based upon dropdown.
 
It should sort the complete column when selected from dropdownlist item.
 
In dropdownlist I used items as
1)sort by Employee name ---This must be sort alphabetwise A-Z
2)Sort by Hiring Date ---- Ascending order
3)Sort by Terminating Date --- Ascending Order.
 
When i select one item from Dropdownlist, that particular column must sort in Gridview.
 

Please help me, and solve this problem.
 
THANKS IN ADVANCE.
Posted 24 Jul '12 - 23:05

Comments
aspnet_regiis - 25 Jul '12 - 5:24
what have you tried?
Sandeep Mewara - 25 Jul '12 - 5:25
And what is the issue here? What have you tried and are stuck with? All you need is to sort the data based on a selection made in dropdown.

1 solution

Hi,
 
Use this:
//Dropdownlist selected value changed event. This can be done on button click event also.
protected void DropDownList1_SelectIndexChanged(object sender, EventArgs e)        {
    gvSorting.Sort(DropDownList1.SelectedValue, SortDirection.Ascending);        
}
 
//Gridview sort command
protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dtSortTable = gvSorting.DataSource as DataTable;
    if (dtSortTable != null)
    {
        DataView dvSortedView = new DataView(dtSortTable);
        dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
        gvSorting.DataSource = dvSortedView;
        gvSorting.DataBind();
    }
}
 
//Getting the sort direction
private static string getSortDirectionString(SortDirection sortDireciton)
{
    string newSortDirection = String.Empty;
    if (sortDireciton == SortDirection.Ascending)
    {
        newSortDirection = "ASC";
    }
    else
    {
        newSortDirection = "DESC";
    }
    return newSortDirection;
}
 

 
All the best.
--Amit
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 428
1 OriginalGriff 291
2 Mayur_Panchal 153
3 Mohammed Hameed 145
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,146
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid