这是我继承的代码,这基本上从数据库加载了所有组合框。我的要求是基于从第一个组合框中选择的值,第二个应被加载。
/// <summary>
/// Pre fill all comboBoxes with items loaded from db
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
private void Upload_Load(object sender, EventArgs e)
{
try
{
fillGroupCombobox();
// load group
//groupIds = new List<string>();
//groupIds = getGroupIds();
if (groupIds.Count != 0)
{
group = new StringBuilder();
string lastItem = groupIds[groupIds.Count - 1];
foreach (string item in groupIds)
{
if (item != lastItem)
group.Append(item + ",");
else group.Append(item);
}
// load commodity
fillCommodityCombobox(group);
fillPhysicalAttribute();
fillProdPracComboBox();
fillUtilPracComboBox();
fillStatTypeCombobox();
}
}
private void fillCommodityCombobox(StringBuilder group)
{
commodities = new List<string>();
string sql = @"SELECT [ERSCommoditySubCommodity_Desc] FROM " + schemaName + "[ERSCommoditySubCommodity_LU] WHERE [ERSCommoditySubCommodity_GroupID] IN ( " + group + " ) ORDER BY [ERSCommoditySubCommodity_Desc]";
DataTable dt = GetData(sql);
DataRow[] dr = dt.Select();
foreach (DataRow row in dr)
{
commodities.Add(row["ERSCommoditySubCommodity_Desc"].ToString());
}
comboBox_commodity.DataSource = commodities;
comboBox_commodity.SelectedItem = null;
}
这是我的尝试,但是每当我尝试时,它都会尝试调用所有组合框,
private void fillCommodityCombobox(StringBuilder group)
{
string selectedGroup = this.comboBox_Group.GetItemText(this.comboBox_Group.Text);
commodities = new List<string>();
string sql = @"SELECT [ERSCommoditySubCommodity_Desc] FROM " + schemaName + "[ERSCommoditySubCommodity_LU],[ERSGroup_LU] WHERE [ERSCommoditySubCommodity_GroupID] = ( " + group + " ) and ERSGroup_Desc= ( " + selectedGroup + " ) ORDER BY [ERSCommoditySubCommodity_Desc]";
DataTable dt = GetData(sql);
DataRow[] dr = dt.Select();
foreach (DataRow row in dr)
{
commodities.Add(row["ERSCommoditySubCommodity_Desc"].ToString());
}
comboBox_commodity.DataSource = commodities;
comboBox_commodity.SelectedItem = null;
}
对此有何想法?