Изменение значения при фильтрации двух выпадающих списков в gridview
Привет
Значение фильтрации изменяется при фильтрации двух выпадающих списков.На самом деле у меня есть два выпадающих списка: 1.Возраст и 2.зарплата ...Если я выберу возраст (20-30 лет) и зарплату (40000-50000), то результат должен прийти при фильтрации обоих.
Что я уже пробовал:
<pre>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // bind(); BindGrid(); BindAge(); BindSalary(); } } protected void bind() { cn.Open(); SqlCommand cmd = new SqlCommand("gvempdetails", cn); cmd.Parameters.AddWithValue("@Action", "SELECT"); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cn.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetails.DataSource = ds; gvDetails.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetails.DataSource = ds; gvDetails.DataBind(); int columncount = gvDetails.Rows[0].Cells.Count; gvDetails.Rows[0].Cells.Clear(); gvDetails.Rows[0].Cells.Add(new TableCell()); gvDetails.Rows[0].Cells[0].ColumnSpan = columncount; gvDetails.Rows[0].Cells[0].Text = "Enter the details"; } } protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Add")) { TextBox txtEmpId = (TextBox)gvDetails.FooterRow.FindControl("txtempid"); TextBox txtEmpName = (TextBox)gvDetails.FooterRow.FindControl("txtempname1"); TextBox txtEmpDep = (TextBox)gvDetails.FooterRow.FindControl("txtdep1"); TextBox txtAge = (TextBox)gvDetails.FooterRow.FindControl("txtage1"); TextBox txtSal = (TextBox)gvDetails.FooterRow.FindControl("txtsal1"); string Id = txtEmpId.Text; string name = txtEmpName.Text; string department = txtEmpDep.Text; string age = txtAge.Text; string salary = txtSal.Text; INSERTEmployee(Id, name, department, age, salary); gvDetails.EditIndex = -1; bind(); } } protected void INSERTEmployee(string Id, string name, string department, string age, string salary) { SqlCommand cmd = new SqlCommand(); cn.Open(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "gvempdetails"; cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@EMPLOYEEID", int.Parse(age.Trim()))); cmd.Parameters.Add(new SqlParameter("@EMPLOYEENAME", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@DEPARTMENT", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@AGE", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@SALARY", int.Parse(salary.Trim()))); cmd.Parameters["@Action"].Value = "INSERT"; cmd.Parameters["@EMPLOYEEID"].Value = Id; cmd.Parameters["@EMPLOYEENAME"].Value = name; cmd.Parameters["@DEPARTMENT"].Value = department; cmd.Parameters["@AGE"].Value = age; cmd.Parameters["@SALARY"].Value = salary; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gvDetails.DataSource = dt; gvDetails.DataBind(); cn.Close(); } protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e) { Label EmpId = (Label)gvDetails.Rows[e.RowIndex].FindControl("lblempid"); TextBox txtEmpName = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtempname"); TextBox txtEmpDep = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtdep"); TextBox txtAge = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtage"); TextBox txtSal = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtsal"); string Id = EmpId.Text; string name = txtEmpName.Text; string department = txtEmpDep.Text; string age = txtAge.Text; string salary = txtSal.Text; UpdateEmployee(Id, name, department, age, salary); gvDetails.EditIndex = -1; bind(); } protected void UpdateEmployee(string Id, string name, string department, string age, string salary) { SqlCommand cmd = new SqlCommand(); cn.Open(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "gvempdetails"; cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@EMPLOYEEID", SqlDbType.Int)); cmd.Parameters.Add(new SqlParameter("@EMPLOYEENAME", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@DEPARTMENT", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@AGE", SqlDbType.VarChar, 30)); cmd.Parameters.Add(new SqlParameter("@SALARY", SqlDbType.Int)); cmd.Parameters["@Action"].Value = "UPDATE"; cmd.Parameters["@EMPLOYEEID"].Value = Convert.ToInt32(Id.ToString()); cmd.Parameters["@EMPLOYEENAME"].Value = name; cmd.Parameters["@DEPARTMENT"].Value = department; cmd.Parameters["@AGE"].Value = age; cmd.Parameters["@SALARY"].Value = Convert.ToInt32(salary.ToString()); //cmd.Parameters["@SALARY"].Value = Convert.ToInt32(salary.ToString()); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gvDetails.DataSource = dt; gvDetails.DataBind(); cn.Close(); } protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e) { gvDetails.EditIndex = e.NewEditIndex; bind(); } protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvDetails.EditIndex = -1; bind(); } protected void DeleteEmployee(string id) { SqlCommand cmd = new SqlCommand(); cn.Open(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "gvempdetails"; cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar, 50)); cmd.Parameters.Add(new SqlParameter("@EMPLOYEEID", int.Parse(id.Trim()))); cmd.Parameters["@Action"].Value = "DELETE"; cmd.Parameters["@EMPLOYEEID"].Value = id; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gvDetails.DataSource = dt; gvDetails.DataBind(); cn.Close(); } protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) { Label EmpId = (Label)gvDetails.Rows[e.RowIndex].FindControl("lblempid"); string id = EmpId.Text; DeleteEmployee(id); gvDetails.EditIndex = -1; bind(); } protected void OnSelectedIndexChanged(object sender, EventArgs e) { } protected void OnPaging(object sender, GridViewPageEventArgs e) { gvDetails.PageIndex = e.NewPageIndex; this.bind(); } protected void BindSalary() { DataTable dt = new DataTable(); cn.Open(); SqlCommand cmd = new SqlCommand("Select * from Salary1", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); //DataSet ds = new DataSet(); da.Fill(dt); cn.Close(); ddlAddSalary1.DataSource = dt; ddlAddSalary1.DataTextField = "Salary"; ddlAddSalary1.DataValueField = "Salary"; ddlAddSalary1.DataBind(); ddlAddSalary1.Items.Insert(0, new ListItem("--Select--")); } protected void ddlAddSalary1_SelectedIndexChanged1(object sender, EventArgs e) { BindGrid(); } protected void BindAge() { DataTable dt = new DataTable(); cn.Open(); SqlCommand cmd = new SqlCommand("Select * from Age", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); //DataSet ds = new DataSet(); da.Fill(dt); cn.Close(); ddlAge.DataSource = dt; ddlAge.DataTextField = "Age"; ddlAge.DataValueField = "Age"; ddlAge.DataBind(); ddlAge.Items.Insert(0, new ListItem("--Select--")); } protected void ddlAge_SelectedIndexChanged(object sender, EventArgs e) { BindGrid(); } protected void gvDetails_OnRowDataBound(object sender, GridViewRowEventArgs e) { } private void BindGrid() { string query = " select * from gvdetails17 "; SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; if (ddlAddSalary1.SelectedValue != "") { string[] sal = ddlAddSalary1.SelectedValue.Split('-'); string from = sal[0]; string to = sal[1]; query += " and Sal between @fromsal and @tosal "; cmd.Parameters.AddWithValue("@fromsal", from); cmd.Parameters.AddWithValue("@tosal", to); } if (ddlAge.SelectedValue != "") { string[] ages = ddlAge.SelectedValue.Split('-'); string from = ages[0]; string to = ages[1]; query += " and Sal between @fromage and @toage "; cmd.Parameters.AddWithValue("@fromage", from); cmd.Parameters.AddWithValue("@toage", to); } cmd.CommandText = query; DataTable dt = new DataTable(); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); gvDetails.DataSource = dt; gvDetails.DataBind(); } } }