Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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:


C#
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
Posted
Updated 14-Jan-14 1:55am
v2

1 solution

C#
if (DropDownList1.SelectedValue == "" && TextBox1.Text == "")
{
//code
}
if (DropDownList1.SelectedValue != "" && TextBox1.Text == "")
{
//code
}
if (DropDownList1.SelectedValue == "" && TextBox1.Text != "")
{
//code
}
if (DropDownList1.SelectedValue != "" && TextBox1.Text != "";)
{
//code
}
 
Share this answer
 
Comments
miss786 14-Jan-14 7:58am    
Many thanks. Just what I need.
SREENATH GANGA 14-Jan-14 8:39am    
good but if else may be little bit faster also check for null value when handling with dropdownlist
miss786 14-Jan-14 9:25am    
Thank you for the advice.

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