Apply Multiple Filters on Gridview in Asp.net C#
  • Hello
    I am a newbie to Asp.Net using C#.
    I have a grid view in C# and i have three drop down lists. I want to filter Gridview on Selectedindex changed when some one selects some value in Dropdown list. I have applied this but currently only One dropSelectedIndex change works at time. i want to apply multiple filters or want to apply a filter on already filtered value. 
    Please help

  • 1 Comment sorted by
  • Vote Up0Vote Down ithackerithacker
    117.50 Karma Accepted Answer
    you can extend filter on down level on your every drop down for example you have table tblUser where you have country, State and city and similarly you have this three drop down on your webpage so you can filter it like below code.
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    SqlConnection objCon = new SqlConnection(@"Data Source=.\sqlExpress;Initial Catalog=dbTest;Integrated Security=SSPI; pooling=false");
    SqlCommand objCmd = new SqlCommand("select * from tblUser where CountryId="+ DropDownList1.SelectedItem.Value, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable();
    objDA.Fill(dt);

    GridView1.DataSource = dt.DefaultView;
    GridView1.DataBind();
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
    SqlConnection objCon = new SqlConnection(@"Data Source=.\sqlExpress;Initial Catalog=dbTest;Integrated Security=SSPI; pooling=false");
    SqlCommand objCmd = new SqlCommand("select * from tblUser where CountryId="+ DropDownList1.SelectedItem.Value+" and StateId="+DropDownList2.SelectedItem.Value, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable();
    objDA.Fill(dt);

    GridView1.DataSource = dt.DefaultView;
    GridView1.DataBind();
    }


    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
    SqlConnection objCon = new SqlConnection(@"Data Source=.\sqlExpress;Initial Catalog=dbTest;Integrated Security=SSPI; pooling=false");
    SqlCommand objCmd = new SqlCommand("select * from tblUser where CountryId="+ DropDownList1.SelectedItem.Value+" and StateId="+DropDownList2.SelectedItem.Value
    +" and CItyId="+DropDownList3.SelectedItem.Value, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable();
    objDA.Fill(dt);

    GridView1.DataSource = dt.DefaultView;
    GridView1.DataBind();
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Top Posters Weekly

Optimum Creative