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 NairPosted Feb 5, 2011, 11:27 AM
shally guptaPosted Feb 5, 2011, 11:19 AM
shally guptaPosted Feb 5, 2011, 11:17 AM
Suthish NairPosted Feb 5, 2011, 3:17 AM
shally guptaPosted Feb 5, 2011, 1:11 AM
Suthish NairPosted Feb 4, 2011, 2:32 PM
What problem you are facing now. SelectedIndex event not firing?
shally guptaPosted Feb 4, 2011, 10:59 AM
dheeraj pkPosted Feb 4, 2011, 7:18 AM
shally guptaPosted Feb 4, 2011, 5:53 AM
dheeraj pkPosted Feb 4, 2011, 3:15 AM
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 guptaPosted Feb 4, 2011, 2:11 AM
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 pkPosted Feb 3, 2011, 6:19 AM
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'.
// Call BindData MethodSuthish NairPosted Feb 3, 2011, 5:48 AM
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
shally guptaPosted Feb 3, 2011, 5:18 AM
dheeraj pkPosted Feb 2, 2011, 9:18 AM
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("'","''"));
Krishna GaradPosted Feb 2, 2011, 12:07 AM