按下拉列表过滤Listview [英] Filtering Listview by Dropdownlists

查看:74
本文介绍了按下拉列表过滤Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我已经要求我的朋友帮我编写过滤工具的编码,但我们在某些部分有库存,所以我需要你的支持人员来帮助我更新下面的代码,例如var query = query + 哪里+条件;还有string conditions =;我们在c#中没有那么大的经验,所以我指的是你们,如果你需要更多解释,下面的youtube链接会提供更多信息请查看



https: //www.youtube.com/watch?v=nh5fbBuUeOE



In my application i have ask my friend to help me with coding the filtering tools but we got stock in some parts, so i need your kind support guys to help me with updating my code below such as "var query = query + " where " + conditions;" also "string conditions = "";" we dont have that big experience in c# so for that i refer to you guys if you need more explanation the below youtube link will give more info please check it

https://www.youtube.com/watch?v=nh5fbBuUeOE

protected void Sortcarbtn_Click(object sender, EventArgs e)
        {

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

                using (SqlConnection CarsortCon = new SqlConnection(cs))
                {

                    string maker = (barndcardrlst.SelectedIndex > 0) ? barndcardrlst.SelectedValue : null;
                    string yearfrom = (CarYearfrmDrDw.SelectedIndex > 0) ? CarYearfrmDrDw.SelectedValue : null;
                    string yearto = (CarYeartoDrDw.SelectedIndex > 0) ? CarYeartoDrDw.SelectedValue : null;
                    string gearddl = (GearDrDw.SelectedIndex > 0) ? GearDrDw.SelectedValue : null;
                    string CarCond = (CarCondDrDw.SelectedIndex > 0) ? CarCondDrDw.SelectedValue : null;
                    string prisfrom = (CarPriceFrmDrDw.SelectedIndex > 0) ? CarPriceFrmDrDw.SelectedValue : null;
                    string pristo = (CarPriceToDrDw.SelectedIndex > 0) ? CarPriceToDrDw.SelectedValue : null;
                    string Contrddl = (countrdrdolst.SelectedIndex > 0) ? countrdrdolst.SelectedValue : null;
                    string statddl = (statedrdolst.SelectedIndex > 0) ? statedrdolst.SelectedValue : null;
                    string cityddl = (citiesdrdolst.SelectedIndex > 0) ? citiesdrdolst.SelectedValue : null;


                    string query = (@"select * from ads where Category = @Category 
                    AND Country = @Country AND Maker=@brand AND Gear=@G AND Condition=@COND AND Year >@startYear and Year <@endYear");

                    var location = Convert.ToString(Session["location"]);
                    var cat = Convert.ToString(Request.QueryString["cat"]);


                    string conditions = "";


                    if (maker != null) conditions += " maker like '%" + maker + "%' ";

                    if (yearfrom != null) conditions += " and year > " + yearfrom;

                    if (yearto != null) conditions += " and year < " + yearto;

                    if (gearddl != null) conditions += " and gear = '%" + gearddl + "%' ";

                    if (CarCond != null) conditions += " Condition = '%" + CarCond + "%' ";

                    if (prisfrom != null) conditions += " and price > " + prisfrom;

                    if (pristo != null) conditions += " and price < " + pristo;

                    if (Contrddl != null) conditions += " Country = '%" + Contrddl + "%' ";

                    if (statddl != null) conditions += " State = '%" + statddl + "%' ";

                    if (cityddl != null) conditions += " City = '%" + cityddl + "%' ";



                    var query = query + " where " + conditions;


                    CarsortQ.Fill(CarsortDataSet);

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

                }

            }
        }

推荐答案

看一看在这篇文章: ADO.NET,正确的方式 [ ^ ]



适用于您要查找的内容的部分从使用参数化查询查询开始。
Take a look at this article: ADO.NET, the right way[^]

The section applicable to what you're looking for starts at "Querying using parameterized queries".


这篇关于按下拉列表过滤Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆