【问题标题】:Loading a combo box data source加载组合框数据源
【发布时间】:2015-06-30 17:26:07
【问题描述】:

所以我想使用一个函数为组合框加载数据源,该函数将需要加载的数据源的名称作为字符串接收,然后让它加载它但是我无法让它工作因为我认为程序只是试图加载变量名而不是它所代表的数据源。抱歉,如果措辞不好,希望我的代码能澄清我的意思。

这就是我现在的做法

    bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
    {
        if (teamName == "Canada")
        {
            string[] players = {"Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" };
            team.DataSource = players;
        }
        else if (teamName == "New Zealand")
        {
            string[] players = {"Dan Carter", "Richie Mccaw", "Julian Savea" };
            team.DataSource = players;
        }
        else if (teamName == "South Africa")
        {
            string[] players = {"Jean de Villiers", "Bryan Habana", "Morne Steyn" };
            team.DataSource = players;
        }
        return (true);
    }

但我想做更多这样的事情

    bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
    {
        string[] Canada = {"Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" };
        string[] NZ = {"Dan Carter", "Richie Mccaw", "Julian Savea" };
        string[] RSA = {"Jean de Villiers", "Bryan Habana", "Morne Steyn" };
        team.DataSource = teamName;
        return (true);
    }

其中 teamName 将是 Canada、NZ 或 RSA。 有谁知道我可以这样做吗?

【问题讨论】:

    标签: c# combobox datasource


    【解决方案1】:

    制作团队名称字典。

    Dictionary<string, string[]> teams = new Dictionary<string, string[]>();
    
    public void PopulateTeams()
    {
        teams.Add("canada", new[] { "Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" });
        teams.Add("nz", new[] { "Dan Carter", "Richie Mccaw", "Julian Savea" });
        teams.Add("rsa", new[] { "Jean de Villiers", "Bryan Habana", "Morne Steyn" });
    }
    

    字典的用法:

    private bool TeamPlayers(string teamName, ComboBox team)
    {
        team.DataSource = null;
        if (teams.ContainsKey(teamName))
        {
            team.DataSource = teams[teamName];
            return true;
        }
        return false;
    }
    

    【讨论】:

    • 感谢您的帮助 =)
    【解决方案2】:

    你可以使用字典来实现类似的功能

    bool TeamPlayers(string teamName, ComboBox team)//Makes the players of the selected team available for selection as scorers
    {
        Dictionary<string, string[]> teamNames = new Dictionary<string, string[]>();
    
        teamNames.Add("Canada", new string[] { "Johny Moonlight", "DTH Van Der Merwe", "Phil Mackenzie" });
        teamNames.Add("New Zealand", new string[] { "Dan Carter", "Richie Mccaw", "Julian Savea" });
        teamNames.Add("South Africa", new string[] { "Jean de Villiers", "Bryan Habana", "Morne Steyn" });
    
        team.DataSource = teamNames[teamName];
        return (true);
    }
    

    【讨论】:

    • 感谢您解决了我的问题。我可以用图像框做同样的事情吗?
    • @JulienPowell 是的,你可以
    • 抱歉,您能否澄清一下,因为显而易见的原因,我无法做到这一点
    • @JulienPowell 您可能想发布您的代码或提出另一个问题,否则我可能会很难回答。另一个问题,因为您的代码在 cmets 中渲染效果不佳。
    • 我打算这样做,但我正在等待 90 分钟的冷却时间,以便我可以再次发布。
    【解决方案3】:

    你可以像这样制作字典:

    dict<string, string[]> teamPlayers;
    

    键是队名,值是球员。

    team.DataSource = teamPlayers[teamName];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2011-06-19
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多