使用多个控件过滤gridview [英] filter gridview using multiple controls

查看:94
本文介绍了使用多个控件过滤gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我正在尝试使用text.box和DDL过滤我的gridview。我希望能够过滤我的搜索,如果搜索textbox1.name = corn和ddl.priceType = talk,girdview应显示带有price_type = talk的玉米结果。



我使用以下代码,我无法使上述逻辑工作:





Dear all,

I am trying to filter my gridview using both text.box and DDL. I would like to be able to filter my search as following, if search for textbox1.name = corn and ddl.priceType = talk, the girdview should display results of corn with price_type = talk.

I am using the following code and I cannot get the above logic to work:


protected void Button1_Click(object sender, EventArgs e)
   {
       if (TextBox1.Text != "")
       {

           String constr = ConfigurationManager.ConnectionStrings["###"].ConnectionString;
           SqlConnection con = new SqlConnection(constr);
           con.Open();
           SqlCommand cmd = new SqlCommand("select [Name], CUSIP, ISINs, [Size (m)],[Current size],priceTalk, [Decimal price], Cover, Type, UploadDate  from [dbo].[database_BWICs] where Name like '%" + TextBox1.Text + "%'");
           cmd.Connection = con;
           cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = TextBox1.Text;
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           GridView1.DataSource = ds;
           GridView1.DataBind();
           Label1.Text = "Found " + Convert.ToString(GridView1.Rows.Count) + " records with the keyword(s): ";
       }

       else if (DropDownList1.SelectedValue != "" && TextBox1.Text != "")
       {
           string query = string.Format("select [Name], CUSIP, ISINs, [Size (m)],[Current size],priceTalk, [Decimal price], Cover, Type, UploadDate  from [dbo].[database_BWICs] where Cover = '{0}' and Name like '%" + TextBox1.Text + "%'", DropDownList1.SelectedValue);
           DataSet dt = GetData(query);
           GridView1.DataSource = dt;
           GridView1.DataBind();

       }

       else
       {
           BindDate(TextDateFrom.Text, TextDateTo.Text);
           Label1.Text = "Found " + Convert.ToString(GridView1.Rows.Count) + " records with the keyword(s): ";
       }

   }





非常感谢



Many thanks

推荐答案

if (DropDownList1.SelectedValue == "" && TextBox1.Text == "")
{
//code
}
if (DropDownList1.SelectedValue != "" && TextBox1.Text == "")
{
//code
}
if (DropDownList1.SelectedValue == "" && TextBox1.Text != "")
{
//code
}
if (DropDownList1.SelectedValue != "" && TextBox1.Text != "";)
{
//code
}


这篇关于使用多个控件过滤gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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