使用 LINQ 从 ListBox 填充 ComboBox
Populate a ComboBox from ListBox with LINQ
我在网上找不到问题的答案..
我有一个使用 LINQ(存储过程)
填充了 SQL table 的列表框
我有一个 ComboBox,它还填充了一个使用 LINQ(存储过程)的 SQL 语句
问题是 ComboBox 的存储过程需要来自 ListBox 的输入,而我似乎无法正确转换它。
这是我的 ListBox 的填充方式:
private void NouvelleReservation2_Load(object sender, EventArgs e)
{
ExamenSGBDEntities oExamenSgbdEntities = new ExamenSGBDEntities();
listBoxRestaurant.DataSource = oExamenSgbdEntities.SelectAllRestaurants();
listBoxRestaurant.DisplayMember = "RESTO";
listBoxRestaurant.ValueMember = "idRestaurants";
listBoxRestaurant.SelectedIndex = 0;
}
这是我的 ComboBox 的填充方式:
private void listBoxRestaurant_SelectedIndexChanged(object sender, EventArgs e)
{
ExamenSGBDEntities oExamenSgbdEntities = new ExamenSGBDEntities();
comboBoxTables.DataSource =
oExamenSgbdEntities.SelectTablesByRestaurant(listBoxRestaurant.Items.Cast<Restaurant>()); //Here is where i'm struggling... I need to give the "idRestaurant" from the ListBox as parameter for my stored procedure.
comboBoxTables.DisplayMember = "TABLECHAISE";
comboBoxTables.ValueMember = "idTables";
}
我曾尝试将 selectedItem 转换为 DataRowView,然后再转换为整数,但这没有用。
已经尝试了您现在在代码中看到的内容,但无法正常工作。
有人可以帮我吗?
提前致谢!
而不是通过:
listBoxRestaurant.Items.Cast()...
你试过了吗?
listBoxRestaurant.SelectedItem
您似乎在尝试将餐厅列表传递给您的查询,但您说您需要 id 值?如果您需要一个 id 值(如在查询中接受一个输入),您应该使用类似:
comboBoxTables.DataSource = oExamenSgbdEntities.SelectTablesByRestaurant(listBoxRestaurant.Items.Cast<Restaurant>().First().idRestaurant);
您可能遇到了转换错误,因为它无法将 IEnumerable 转换为 int。
您也可以将 SelectedItem 转换为 Restaurant,然后按照 Jon Vote 所说的获取 id。
不要尝试投射选定的列表项。相反,使用选定的值:
listBoxRestaurant.SelectedValue
然后您可以通过此 ID 值从数据库中加载餐厅并将其传递给您的 SelectTablesByRestaurant()
函数。
我在网上找不到问题的答案..
我有一个使用 LINQ(存储过程)
填充了 SQL table 的列表框我有一个 ComboBox,它还填充了一个使用 LINQ(存储过程)的 SQL 语句 问题是 ComboBox 的存储过程需要来自 ListBox 的输入,而我似乎无法正确转换它。
这是我的 ListBox 的填充方式:
private void NouvelleReservation2_Load(object sender, EventArgs e)
{
ExamenSGBDEntities oExamenSgbdEntities = new ExamenSGBDEntities();
listBoxRestaurant.DataSource = oExamenSgbdEntities.SelectAllRestaurants();
listBoxRestaurant.DisplayMember = "RESTO";
listBoxRestaurant.ValueMember = "idRestaurants";
listBoxRestaurant.SelectedIndex = 0;
}
这是我的 ComboBox 的填充方式:
private void listBoxRestaurant_SelectedIndexChanged(object sender, EventArgs e)
{
ExamenSGBDEntities oExamenSgbdEntities = new ExamenSGBDEntities();
comboBoxTables.DataSource =
oExamenSgbdEntities.SelectTablesByRestaurant(listBoxRestaurant.Items.Cast<Restaurant>()); //Here is where i'm struggling... I need to give the "idRestaurant" from the ListBox as parameter for my stored procedure.
comboBoxTables.DisplayMember = "TABLECHAISE";
comboBoxTables.ValueMember = "idTables";
}
我曾尝试将 selectedItem 转换为 DataRowView,然后再转换为整数,但这没有用。 已经尝试了您现在在代码中看到的内容,但无法正常工作。
有人可以帮我吗?
提前致谢!
而不是通过:
listBoxRestaurant.Items.Cast()...
你试过了吗?
listBoxRestaurant.SelectedItem
您似乎在尝试将餐厅列表传递给您的查询,但您说您需要 id 值?如果您需要一个 id 值(如在查询中接受一个输入),您应该使用类似:
comboBoxTables.DataSource = oExamenSgbdEntities.SelectTablesByRestaurant(listBoxRestaurant.Items.Cast<Restaurant>().First().idRestaurant);
您可能遇到了转换错误,因为它无法将 IEnumerable 转换为 int。
您也可以将 SelectedItem 转换为 Restaurant,然后按照 Jon Vote 所说的获取 id。
不要尝试投射选定的列表项。相反,使用选定的值:
listBoxRestaurant.SelectedValue
然后您可以通过此 ID 值从数据库中加载餐厅并将其传递给您的 SelectTablesByRestaurant()
函数。