加载组合框数据源
Loading a combo box data source
所以我想使用一个函数为组合框加载数据源,该函数以字符串形式接收它需要加载的数据源的名称,然后让它加载它,但是我无法得到这个工作,因为我认为程序只是试图加载变量名而不是它代表的数据源。抱歉,如果措辞不当,希望我的代码能说明我的意思。
我现在就是这样做的
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。
有谁知道我可以这样做的方法吗?
你可以像这样制作字典:
dict<string, string[]> teamPlayers;
键是队名,值是球员。
team.DataSource = teamPlayers[teamName];
您可以使用字典来实现类似的功能
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);
}
制作团队名称词典。
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;
}
所以我想使用一个函数为组合框加载数据源,该函数以字符串形式接收它需要加载的数据源的名称,然后让它加载它,但是我无法得到这个工作,因为我认为程序只是试图加载变量名而不是它代表的数据源。抱歉,如果措辞不当,希望我的代码能说明我的意思。
我现在就是这样做的
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。 有谁知道我可以这样做的方法吗?
你可以像这样制作字典:
dict<string, string[]> teamPlayers;
键是队名,值是球员。
team.DataSource = teamPlayers[teamName];
您可以使用字典来实现类似的功能
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);
}
制作团队名称词典。
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;
}