本文介绍了所选部门的下拉列表未过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我在网格视图中使用了3个下拉列表来过滤部门Ddl没有过滤部门
我尝试过:
Hi I have used 3 dropdownlist in grid view to filter the Department Ddl is not filtering the department
What I have tried:
<pre>private void BindGrid()
{
DataTable dt = new DataTable();
string query = " select * from gvdetails17 WHERE 1=1 ";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
if (ddlAddSalary1.SelectedValue != "" && ddlAddSalary1.SelectedValue != "--Select--")
{
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 != "" && ddlAge.SelectedValue != "--Select--")
{
string[] age = ddlAge.SelectedValue.Split('-');
string from1 = age[0];
string to1 = age[1];
query += " and Age between @fromage and @toage ";
cmd.Parameters.AddWithValue("@fromage", from1);
cmd.Parameters.AddWithValue("@toage", to1);
}
if (ddlDepartment.SelectedValue == "")
{
cmd.Parameters.AddWithValue("@DepartmentName", ddlDepartment.SelectedValue);
}
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void txt_SearchName_TextChanged1(object sender, EventArgs e)
{
cn.Open();
SqlCommand cmd = new SqlCommand("select * from gvdetails17 where EmpName LIKE '% " + txt_SearchName.Text + "%'", cn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
gvDetails.DataSource = dt;
cn.Close();
}
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
推荐答案
if (ddlDepartment.SelectedValue != "")
{
query += " and DepartmentName = @DepartmentName ";
cmd.Parameters.AddWithValue("@DepartmentName", ddlDepartment.SelectedValue);
}
这篇关于所选部门的下拉列表未过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!