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.
Loading

Suthish NairRank #92 · 22.8K reputationPosted Feb 5, 2011, 6:27 AM EST
Krishna GaradRank #191 · 12.8K reputationPosted Feb 1, 2011, 7:07 PM EST
dheeraj pk164 reputationPosted Feb 2, 2011, 4:18 AM EST
You can use Method Overloading..
private void BindData(string where clause)
{
string strQuery=string.Empty;
if(!IsNullorEmpty(whereclause)
{
strQuery="Select * from emp2 Where "+whereclause;
} else
{
strQuery="Select * from emp2";
}
SqlConnection con = new SqlConnection("Data Source=MANISH;Initial Catalog=AdventureWorks;Integrated Security=True");
SqlDataAdapter dp = new SqlDataAdapter(strQuery, con);
DataSet ds = new DataSet();
dp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
// Call Bind Method Like This BindData("Where ename=youdatastring.Replace("'","''"));
shally gupta15 reputationPosted Feb 3, 2011, 12:18 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 3, 2011, 12:48 AM EST
Update your correct name or the Admins here will block your id.
First create a sample sql statement in editor window with out an try that in your logic.
Or, refer article:
Have samples how to retrive rows and bind to gridview.
AJAX AutoCompleteExtender - Dropdownlist like behavior
dheeraj pk164 reputationPosted Feb 3, 2011, 1:19 AM EST
As per u code i think you want filter the gridview with Name(ename) .
for example filter gridview with name =abc.. so where clause should like this... ename='abc'.
shally gupta15 reputationPosted Feb 3, 2011, 9:11 PM EST
private void BindData1()
{
SqlConnection con = new SqlConnection("Data Source=MANISH;Initial Catalog=AdventureWorks;Integrated Security=True");
SqlDataAdapter dp = new SqlDataAdapter("Select * from emp2 where ename ="+ name.SelectedItem.Text.Replace("'","''"), con);
DataSet ds = new DataSet(); dp.Fill(ds,"emp2");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void name_SelectedIndexChanged(object sender, EventArgs e)
{
BindData1();
}
Plz. help
dheeraj pk164 reputationPosted Feb 3, 2011, 10:15 PM EST
SqlDataAdapter dp = new SqlDataAdapter("Select * from emp2 where ename ="+ name.SelectedItem.Text.Replace("'","''"), con);
Ename is varchar type.. so u must add single qoutes..
SqlDataAdapter dp = new SqlDataAdapter("Select * from emp2 where ename ='"+ name.SelectedItem.Text.Replace("'","''")+"'", con);
** If problem exist still please send your code..(only filtering code..)
shally gupta15 reputationPosted Feb 4, 2011, 12:53 AM EST
dheeraj pk164 reputationPosted Feb 4, 2011, 2:18 AM EST
shally gupta15 reputationPosted Feb 4, 2011, 5:59 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 4, 2011, 9:32 AM EST
What problem you are facing now. SelectedIndex event not firing?
shally gupta15 reputationPosted Feb 4, 2011, 8:11 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 4, 2011, 10:17 PM EST
shally gupta15 reputationPosted Feb 5, 2011, 6:17 AM EST
shally gupta15 reputationPosted Feb 5, 2011, 6:19 AM EST