62,046
社区成员
发帖
与我相关
我的任务
分享
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gvbing();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList1.SelectedValue != null)
{
dt = ReturnDataTable("select * from table where 列='" + this.DropDownList1.SelectedValue + "'");
}
else
{
dt = ReturnDataTable("select * from table");
}
Gvbing();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList2.SelectedValue != null && this.DropDownList1.SelectedValue != null)
{
dt = ReturnDataTable("select * from table where 列='" + this.DropDownList1.SelectedValue + "' and 列='" + this.DropDownList2.SelectedValue + "'");
}
else if (this.DropDownList2.SelectedValue == null && this.DropDownList1.SelectedValue != null)
{
dt = ReturnDataTable("select * from table where 列='" + this.DropDownList1.SelectedValue + "'");
}
else
{
dt = ReturnDataTable("select * from table");
}
Gvbing();
}
public void Gvbing()
{
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
/// <summary>
/// 执行有参的查询 返回DataTable
/// </summary>
/// <returns>返回DataTable</returns>
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}