我想用所有国家/地区名称列表填充组合框
Using System.Globalization
Using System.Collections
这是我为 ComboBox 编写的 XAML 代码
<ComboBox x:Name="countryComboBox" Width="300"/>
在 C# 编码中我有添加按钮。这是该代码,我想从 ComboBox 中获取国家/地区并将其存储在 MySql 数据库中。我该怎么做?
private void addButton_Click(object sender, RoutedEventArgs e)
{
try
{
string Query = @"INSERT INTO `bcasdb`.`tbl_student`
(`reg_id`,
`std_fname`,
`std_lname`,
`tbl_batch_batch_id`,
`gender`)
VALUES (@regId, @fName, @lName, @bID, @gender, @country)";
//This is command class which will handle the query and connection object.
MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
MySqlCommand cmd = new MySqlCommand(Query, conn);
conn.Open();
cmd.Parameters.AddWithValue("@regId", this.regIDInput.Text);
cmd.Parameters.AddWithValue("@fName", this.fnameInput.Text);
cmd.Parameters.AddWithValue("@lName", this.lnameInput.Text);
cmd.Parameters.AddWithValue("@bID", this.batchIDInput.Text);
cmd.Parameters.AddWithValue("@gender",this.maleRadioButton);
cmd.ExecuteNonQuery();
conn.Close();
successmsgBox();
}
catch (Exception)
{
errormsgBox();
}
}
请您参考如下方法:
你可以像这样填充组合框,
添加使用System.Globalization
像这样设置 Combobox ItemsSource,
countryComboBox.ItemsSource = GetCountryList();
public static List<string> GetCountryList()
{
List<string> cultureList = new List<string>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region.EnglishName);
}
}
return cultureList;
}
您可以只获取 selectedItem 并将其与查询一起传递,
cmd.Parameters.AddWithValue("@country", this.countryComboBox.SelectedItem.ToString());
编辑: 您已经提到它适用于 Windows 应用商店应用程序,请检查此链接。如何加载国家/地区
Replacement for CultureInfo.GetCultures in .NET Windows Store apps
如果没有,您必须编写自定义方法从数据库中加载国家/地区。
