【问题标题】:Combobox items based on another combobox selected items data from SQL Server database基于 SQL Server 数据库中另一个组合框选定项数据的组合框项
【发布时间】:2020-04-22 17:53:44
【问题描述】:

我有两个组合框,其中第一个具有我从 SQL Server 数据库填充的类别。诀窍是让第二个组合框仅显示数据库中与第一个组合框中所选类别相关联的项目。

这里是我的 SQL 代码:

IF @ActionType = 'FetchDataCBOCity'  
BEGIN  
    SELECT DISTINCT ID_City, Name_City 
    FROM City
END

IF @ActionType = 'FetchDataCBOState'  
BEGIN  
    SELECT ID_State, Name_State 
    FROM State 
END

这是我的 C# 代码:

ConnectionTCP.CboFetchData(new List<string> { "FetchDataCBOCity", spName }, "ID_City", "Name_City", comboBox1);
ConnectionTCP.CboFetchData(new List<string> { "FetchDataCBOState", spName }, "ID_State", "Name_State", comboBox2);


 // CBO Fetch Data in db
        public static void CboFetchData( List<string> dataList, string valueMember, string displayMember, ComboBox cbo )
        {
            try
            {
                string phrase = "CBOFETCHDATA" + ">";
                foreach (var data in dataList)
                {
                    phrase += data + ">";
                }

                byte[] message = Encoding.ASCII.GetBytes(phrase.TrimEnd('>'));
                stream.Write(message, 0, message.Length);

                var buffer = getData(tcpClient);

                cbo.DataSource = DataFormatter.DeserializeData(buffer);
                cbo.ValueMember = valueMember;
                cbo.DisplayMember = displayMember;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.Message, Client.nameApp, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

谢谢

【问题讨论】:

  • 我理解你的问题,但我不能赞成你的问题的唯一原因,听起来,这是我的代码如何做到这一点。比如,WTH 是 ConnectionTCP.CboFetchData( 吗?这不是标准的网络功能,不是吗?不过,我已经提供了如何做的答案
  • 没问题。很高兴问题得到解决。当问题不是特定于企业时,它对社区更有用。例如,“如何填充不同对象的列表” vs “如何制作主人和宠物的集合”
  • 对不起,我添加了您要求的代码。谢谢!

标签: c# winforms combobox


【解决方案1】:

我想,最简单的方法是将DataSet 与相关的DataTables 一起使用,但您可以创建相关的数据源

// Declare combo items
public class ParentInfo
{
    int Id {get; set;}
    string Display {get; set;}
}

public class ChildInfo
{
    int Id {get; set;}
    int ParentId {get; set;}
    string Display {get; set;}
}

// Load lists
List<ParentInfo> _parents;
List<ChildInfo> _children;
// . . . 
// Set parent combo, this one is not changing
cboParents.ValueMemeber = "Id";
cboParents.DisplayMemeber = "Display";
cboParents.DataSourse = _parents;

// . .  . . . 
// Change children for different parents
void OnParent_SelectedIndexChanged( . .  . .)
{
    if (cboParents.SelectedInfex = -1)
    {
        cboChildren.DataSource = null;
        return;
    }
    cboChildren.ValueMemeber = "Id";
    cboChildren.DisplayMemeber = "Display";
    ParentInfo currentP = (ParentInfo)cboParents.SelectedItem;
    cboChildren.DataSourse = _children.Where(c => c.ParentId == currentP.Id).ToList();

}



【讨论】:

  • 谢谢,我刚刚知道怎么做(10 分钟前...)。但是,是的!你说得对,我就是这样做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2016-01-29
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多