Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
The below code its working fine with me to sort the car brand and the Gear, but also i have two dropdownlists (DropDownList3 , DropDownList6) which is allow user to choose the car year made from a column in database table called Year, for example: from 2008 to 2012. so how i can update my code below to get the selected value from this two dropdownlist and gives the records between this two selected values

i have upload a screen record i wish that will explain more about what i am looking for:

https://www.youtube.com/watch?v=nh5fbBuUeOE&feature=youtu.be

C#
protected void Sortcarbtn_Click(object sender, EventArgs e)
        {

            if (Session["location"] != null)
            {

                using (SqlConnection CarsortCon = new SqlConnection(cs))
                {
                    CarsortCon.Open();
                    using (var CarsortQ = new SqlDataAdapter(@"SELECT DISTINCT AdsID, Section, Category, Year, Country, State, City, AdsTit, Maker,Gear, 
                        SUBSTRING(AdsDesc,1,155) as AdsDesc, AdsPrice, Img1 FROM ads WHERE Category = @Category AND Country = @Country AND Maker=@brand AND Gear=@G", cs))
                    {
                        var location = Convert.ToString(Session["location"]);
                        var cat = Convert.ToString(Request.QueryString["cat"]);

                        var CarsortDS = new DataSet();
                        CarsortQ.SelectCommand.Parameters.AddWithValue("@Category", cat);
                        CarsortQ.SelectCommand.Parameters.AddWithValue("@Country", location);
                        CarsortQ.SelectCommand.Parameters.AddWithValue("@brand", catcardrlst.SelectedValue);
                        CarsortQ.SelectCommand.Parameters.AddWithValue("@G", DropDownList1.SelectedValue);


                        CarsortQ.Fill(CarsortDS);

                        cateshowlistview.DataSource = CarsortDS.Tables[0];
                        cateshowlistview.DataBind();


                    }
                }
            }
Posted
Updated 30-Jul-14 19:04pm
v3

1 solution

You can sort in DataTable using DataView by simple passing columnName and ASC or DESC order.. :)

C#
 DataView dv = dtPreSort.DefaultView;

     string SortField = "ColumnName"; //Pass it dynamically..
     string SortExpression = "ASC OR DESC";

 dv.Sort = SortField+" "+SortExpression;
DataTable dtGetShipments=new  DataTable(); 
 dtGetShipments = dv.ToTable();
 
Share this answer
 
v2
Comments
Member 10690878 31-Jul-14 2:50am    
Hi Nirav Prabtani. Thanks for your reply but i am looking for filtering from code behind not sorting i may make mistake in my post title.

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