加载组合框数据源
问题描述:
所以我想加载一个组合框的数据源与使用一个函数接收作为一个字符串的数据源的名称,它需要加载,然后让它加载它然而我无法得到这个工作,因为我认为该程序只是试图加载变量名称,而不是它所代表的数据源。对不起,如果这是措辞不良,希望我的代码清除我的意思。加载组合框数据源
这是我过得怎么样,现在
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将是要么加拿大,新西兰或RSA。 有谁知道我可以做到这一点的方式?
答
您可以使用字典来实现类似的功能
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);
}
答
你可以做一本字典,像这样:
dict<string, string[]> teamPlayers;
主要是teamname,价值将是玩家。
team.DataSource = teamPlayers[teamName];
答
让球队的名字的字典。
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;
}
+0
感谢您的帮助=) –
感谢的是解决我的问题。我能用相机盒做同样的事吗? –
@JulienPowell是的,你可以 –
对不起,但你可以澄清一点,因为我很难做到这一点 –