如何在C#中将两个组合框链接在一起. [英] how do I link two Comboboxes together in C#.

查看:156
本文介绍了如何在C#中将两个组合框链接在一起.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中将两个组合框链接在一起,这两个组合框绑定到数据集.

解决方案


首先,您必须填写 Requesters ,然后将您的代码下拉为


 Page_Load()
{
SQL = " 
SQLDataAdapter da =  SQLDataAdapter(SQL,连接);
DataSet ds =  DataSet();
da.Fill(ds);
drpRequesters.DataTextField = " ;
drpRequesters.DataValueField = " ;
drpRequesters.DataSource = ds.Tables [ 0 ];
drpRequesters.DataBind();
FillLocation();
} 




现在您必须根据部门选择填写位置下拉

 私有 无效 FillLocation()
{
  如果(drpRequesters.SelectedIndex >  =  0 )
  {
drpLocation.Items.Clear();
    SQL = " 中的位置+ drpRequesters.SelectedItem.Value;
SQLDataAdapter da =  SQLDataAdapter(SQL,连接);
DataSet ds =  DataSet();
da.Fill(ds);
drpLocation.DataTextField = " ;
drpLocation.DataValueField = " ;
drpLocation.DataSource = ds.Tables [ 0 ];
drpLocation.DataBind();
  }
} 



最后一步是创建部门变更事件,如

 drpDepartment_SelectedIndexChanged()
{
 FillLocation();
} 



您应该为.aspx页面中的drpDepartment设置

 AutoPostback ="True" 


谢谢,
Imdadhusen


我建​​议按照以下步骤操作

1.将数据从请求者"表中获取到数据集中
2.将数据从位置"表获取到同一数据集中
3.创建与上表中的记录相关的数据关系
4.为Requester和Location表创建2个绑定源,并将Requester的数据源设置为数据集,并将Location设置为Requester数据源,并将Location的数据成员设置为在步骤3中创建的数据关系. 5.将数据源设置为相应的组合框.

您可以在此处看到详细的示例.

-Vivek


how do I link two Comboboxes together in C# that the two Comboboxes binding to a dataset.

解决方案

Hi
First of all you have to fill Requesters drop down your code is like


Page_Load()
{
SQL = "SELECT DISTINCT DepartmentID,Department FROM Requesters"
SQLDataAdapter da = new SQLDataAdapter(SQL, connection);
DataSet ds = new DataSet();
da.Fill(ds);
drpRequesters.DataTextField = "Department";
drpRequesters.DataValueField = "DepartmentID";
drpRequesters.DataSource = ds.Tables[0];
drpRequesters.DataBind();
FillLocation();
}




Now you have to fill Location drop down based on Department selection

private void FillLocation()
{
  if(drpRequesters.SelectedIndex >= 0)
  {
drpLocation.Items.Clear();
    SQL = "SELECT DISTINCT LocationID,Location FROM Locations WHERE DepartmentID=" + drpRequesters.SelectedItem.Value;
SQLDataAdapter da = new SQLDataAdapter(SQL, connection);
DataSet ds = new DataSet();
da.Fill(ds);
drpLocation.DataTextField = "Location";
drpLocation.DataValueField = "LocationID";
drpLocation.DataSource = ds.Tables[0];
drpLocation.DataBind();
  }
}



Final step is you have create change event of Dipartment like

drpDepartment_SelectedIndexChanged()
{
 FillLocation();
}



You should set

AutoPostback="True" 

for drpDepartment from .aspx page

Thanks,
Imdadhusen


I suggest follow these steps

1. Get data from the Requesters table into a dataset
2. Get data from the Location table into the same dataset
3. Create a data relation relating the records in the above tables
4. Create 2 binding source for Requester and Location table and set the datasource of Requester to the dataset and Location to Requester datasource, and datamember of Location to the datarelation created in the step 3.
5. Set the datasource to respective comboboxes.

You can see a detailed example here.

- Vivek


这篇关于如何在C#中将两个组合框链接在一起.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆