开发者

c# Ordering Variable data using a drop down list

I am currently stuck on a small problem.

I have a List View which displays products and I am looking to include a drop down list, with a filter button which when pressed will order the products by high-low price or low-hi开发者_JS百科gh.

Here is my code.

protected void LB_Filter_Click(object sender, EventArgs e)
{
    using (DataClasses_ECDataContext db = new DataClasses_ECDataContext())
    {
        DT_Product Pro = db.DT_Products.SingleOrDefault(x => x.ProductID == int.Parse(ViewState["ProductID"].ToString()));

        var product = from x in db.DT_Products
                      where x.RangeID == Pro.RangeID
                      select new
                      {
                          x.ProductName,
                          x.ProductID,
                          x.Sale_Price,
                          Link = RouteTable.Routes.GetVirtualPath(null, "Product-by-tag", codesnippets.RouteLink(x.ProductID, x.ProductName, 'p')).VirtualPath,
                      };

        if (DDL_Order.SelectedIndex == 0)
        {
            product.OrderByDescending(v => v.Sale_Price);
        }

        else if (DDL_Order.SelectedIndex == 1)
        {
            product.OrderBy(v => v.Sale_Price);
        }

        LV_Products.DataSource = product;
        LV_Products.DataBind();
    }
}

Any Help would be fantastic,

Thanks!


You need to execute the results by converting it to list to get the expected output, like this:

        List<DT_Product> products = new List<DT_Product>();
        if (DDL_Order.SelectedIndex == 0) { 
            products = product.OrderByDescending(v => v.Sale_Price).ToList(); 
        } else if (DDL_Order.SelectedIndex == 1) {
            products = product.OrderBy(v => v.Sale_Price).ToList(); 
        }


        LV_Products.DataSource = products; 
        LV_Product.DataTextField = "ProductName"; 
        LV_Product.DataValueField = "ProductID";
        LV_Products.DataBind();


Are you missing this?

LV_Products.DataSource = product;
LV_Product.DataTextField="ProductName";
LV_Product.DataValueField="ProductID";
LV_Products.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜