shally gupta

shally gupta

  • NA
  • 15
  • 0

gridview filteration

Feb 2 2011 12:02 AM
Hi, I have a problem in filtering gridview based on combo box. I have two seperate controls on form i.e. combo box and gridview. I want when i select some value from combo box, grid view should display only those records. My code is
   protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }
       private void BindData()
        {
        SqlConnection con = new SqlConnection("Data Source=MANISH;Initial Catalog=AdventureWorks;Integrated Security=True");
        SqlDataAdapter dp = new SqlDataAdapter("Select * from emp2", con);
        DataSet ds = new DataSet();
        dp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        }
  
//  combobox_ID =Name
    protected void combobox_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MANISH;Initial Catalog=AdventureWorks;Integrated Security=True");
        con.Open();
        string empname;
        empname = name.SelectedItem.Text;
        //empname = TextBox1.Text;
        string query = "select * from emp2 where ename=@ename";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.Add(new SqlParameter("@ename", empname));
        cmd.ExecuteNonQuery();
        con.Close();
        //GridView1.DataBind();
        BindData();
        Lbtext.Text = "query executed";
    }

can u plz help.


Answers (16)