日期:2014-05-19  浏览次数:20379 次

在下拉列表框中选择一个值,在DataGrid中显示相应的信息
一个下拉框DropDownList1
一个DataGrid1
要求在DropDownList1中选择一个,假如选的是“机器一”
则在DataGrid1中显示   机器一   的有关运行情况


------解决方案--------------------
设置dropdownlist1的autopostback为true

然后给dropdownlist1加onselectchanged事件.

事件里写sql语句,重新绑定datagrid
------解决方案--------------------
你把绑定数据的方法给各参数,是从组合框得来的内容不就ok了

private void BindDataGrid(string strField)
{
SqlConnection cn;
SqlCommand cmd;
SqlDataReader dr;

cn = new SqlConnection(connectionString);
string strSQL;
if (strField.Length == 0)
strSQL = "Select * From Note ";
else
strSQL = "Select * From Note where yourfield = " + strField;

cmd = new SqlCommand(strSQL, cn);
cn.Open();
dr = cmd.ExecuteReader();

DataGrid1.DataSource = dr;
DataGrid1.DataBind();

dr.Close();
cn.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
BindDataGrid( " ");
}
}

//记得把DropDownList1的AutoPostBack属性设置为true
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindDataGrid(DropDownList1.SelectedItem.Text);
}
------解决方案--------------------
在dropdownlist1的onSelectChanged事件中根据dropdownlist1.selectItem.text()的值查询到的响应的数据填充到dropdownlist2,重新绑定到datagrid,记得把dropdownlist1的autopostback改为true

------解决方案--------------------
DropDownList1的SelectedIndexChanged事件中,调用 BindGrid方法 BindGrid方法中 根据DropDownList1.SelectedValue来查询数据并绑定.
------解决方案--------------------
据DropDownList1.SelectedValue来查询数据并绑定到DataGrid中.

------解决方案--------------------
http://singlepine.cnblogs.com/articles/266538.html这里有个很全面的例子,自己多研究研究