KYQ
KYQ

Reputation: 53

how to sort gridview based on drop down list selection

I need the gridview to be displayed based on the dropdownlist : LastestTransactionFirst, EarlierTransactionFirst. So basically it's desc or asc based on the date in gridview. May i know how should i go about it ?

This is my code for gridview selection. But I had a dropdownlist and need the gridview to appear based on the dropdownlist selection.

    myConnection.ConnectionString = strConnectionString;
    SqlCommand cmd = new SqlCommand("SELECT thDate, thType, thAmountIn, thAmountOut from [Transaction] ORDER BY thDate, thType, thAmountIn, thAmountOut DESC", myConnection);
    myConnection.Open();
    SqlDataReader reader1 = cmd.ExecuteReader();
    GridView1.DataSource = reader1;
    GridView1.DataBind();

Upvotes: 0

Views: 3068

Answers (2)

Zo Has
Zo Has

Reputation: 13028

1) You can use Dynamic Order By in your query preferably using a stored procedure

2) Quick & dirty way would be passing the selectedValue on your dropdown list selected index changed event & re-binding your gridview, also enable autopostback to true on the dropdown list. Something like

protected void yourDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
   BindYourGridView(yourDropDown.SelectedValue);  
}

BindYourGridView(string sortParam)
{
   string orderBy=null;
   switch sortParam
   {
         case 0:
             orderBy= "ORDER BY thDate, thType, thAmountIn, thAmountOut DESC"
             break;
         case 1:
             orderBy= "ORDER BY thDate, thType, thAmountIn, thAmountOut"
             break;   
   }
  string yourQuery= "Select columns from table "+ orderBy;
  // Your data access code
  // Bind your gridview
}


//ASPX
   <asp:DropDownList ID="yourDropDownList" runat="server" AutoPostBack="True">
   <asp:ListItem Text="Recent First" Value="0" />
   <asp:ListItem Text="Earlier First" Value="1" />
   </asp:DropDownList>

Upvotes: 1

Krishna Thota
Krishna Thota

Reputation: 7026

This is an example of sorting in gridview

DataView dvItems = new DataView((DataTable)ds.Tables["datatable1"]);

 if (ddl_itemsorderby.SelectedValue == "MenuGroup")
     dvItems.Sort = "Menu_Group, Item_Name ASC";
 else if (ddl_itemsorderby.SelectedValue == "Item")
     dvItems.Sort = "Item_Name, Menu_Group ASC";
 else if (ddl_itemsorderby.SelectedValue == "Rate")
     dvItems.Sort = "Item_rate, Item_Name ASC";
 else if (ddl_itemsorderby.SelectedValue == "Quantity")
     dvItems.Sort = "Item_Quantity, Item_Name ASC";

 gridview1.DataSource = dvItems;
 gridview1.DataBind();

Here Menu_Group,Item_name ... are the column names in the datatable which you bind to the gridview.

"Menu_Group, Item_Name ASC" This means that The forse order prority will be given to the Menu_Group first and second order priority will be given to Item_Name. ASC is the Order type will be ascending

ddl_itemsorderby is the dropdownlist from which the column order will be selected.
dvitems is the dataview.

ASC is the ascending order.

You have to create a dataview containing the data in the datatable and then sort the values in the dataview and then bind the dataview to the gridview

Upvotes: 0

Related Questions