根据第一个 ComboBox (1) c# 的输入,从 ComboBox (2) 中消除一个选项
Eliminating an option from a ComboBox (2), based on the input of the first ComboBox (1) c#
我正在创建一个航空公司预订系统,我有 2 个组合框。第一个是出发城市,第二个是到达城市。我希望能够从第二个组合框中消除第一个组合框中的选择,因为我不希望同一个城市能够作为出发城市和到达城市提交。我正在从数据库中查询城市名称。
这是我的代码:
public partial class main : Form
{
public main()
{
InitializeComponent();
string connectionString = @"Base Schema Name=cyanair;data source=C:\Users\Client 0819\source\repos\Cyanair\cyanair.db";
//Departure ComboBox
SQLiteConnection conn = new SQLiteConnection(connectionString);
try
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT * FROM CyanairAirports";
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboDeparture.DataSource = dt;
comboDeparture.ValueMember = "Descriptions";
comboDeparture.DisplayMember = "Descriptions";
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Arrival ComboBox
private void comboDeparture_DisplayMemberChanged(object sender, EventArgs e)
{
string connectionString = @"Base Schema Name=cyanair;data source=C:\Users\Client 0819\source\repos\Cyanair\cyanair.db";
SQLiteConnection conn = new SQLiteConnection(connectionString);
**String city = comboDeparture.DisplayMember;**
try
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT * FROM CyanairAirports WHERE Descriptions IS NOT '" + comboDeparture.SelectedValue.ToString() + "'";
richTextBox1.Text = "SELECT * FROM CyanairAirports WHERE Descriptions IS NOT '" + comboDeparture.SelectedValue + "'";
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboArrival.DataSource = dt;
comboArrival.ValueMember = "Descriptions";
comboArrival.DisplayMember = "Descriptions";
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
谢谢:)
您似乎正在处理 DisplayMemberChanged
event on comboDeparture
, and trying to update the values of comboArrival
in that handler. However, DisplayMemberChanged
only triggers when the DisplayMember
属性 更改。
DisplayMember
仅告诉控件在数据绑定控件上显示哪个 属性。它与 ComboBox
中选择的索引或值无关。因此,唯一一次填充 comboArrival
运行s 的代码是在您设置 comboDepartarture.DisplayMember
时的构造函数中。相反,处理 ComboBox.SelectedIndexChanged
or ComboBox.SelectedValueChanged
并设置 comboArrival
.
的项目
关于您的代码需要注意的其他一些重要事项。
首先,当 运行ning Sql 语句时应该使用参数化查询,而不是连接字符串。在您进行操作时连接字符串可以帮助您 SQL Injection Attacks. I'm not familiar with SqlLite and can't provide you with an example of how to modify your code, but perhaps this question。
其次,您不需要在每次更改 comboDeparture
中的选定值时重新运行 查询。只需将 comboArrival
的数据源添加为 Form
上的字段即可对其进行过滤。例如...
public partial class main : Form
{
// Your constructors...
private void comboDepartures_SelectedIndexChanged(object sender, EventArgs e)
{
if (_arrivalsDataSource == null)
{
_arrivalsDataSource = new System.Data.DataTable();
// Load _arrivalsDataSource from the database, basically how you're doing it now.
comboArrival.DataSource = _arrivalsDataSource.DefaultView;
comboArrival.DisplayMember = "Descriptions"
comboArribal.ValueMember = "Descriptions"
}
if (comboDeparture.SelectedIndex == -1)
{
_arrivalsDataSource.DefaultView.RowFilter = null; // Clear the filter.
}
else
{
// Set the filter.
_arrivalsDataSource.DefaultView.RowFilter = $"Description <> '{comboDeparture.SelectedValue}'";
}
}
private System.Data.DataTable _arrivalsDataSource = null;
}
我正在创建一个航空公司预订系统,我有 2 个组合框。第一个是出发城市,第二个是到达城市。我希望能够从第二个组合框中消除第一个组合框中的选择,因为我不希望同一个城市能够作为出发城市和到达城市提交。我正在从数据库中查询城市名称。
这是我的代码:
public partial class main : Form
{
public main()
{
InitializeComponent();
string connectionString = @"Base Schema Name=cyanair;data source=C:\Users\Client 0819\source\repos\Cyanair\cyanair.db";
//Departure ComboBox
SQLiteConnection conn = new SQLiteConnection(connectionString);
try
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT * FROM CyanairAirports";
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboDeparture.DataSource = dt;
comboDeparture.ValueMember = "Descriptions";
comboDeparture.DisplayMember = "Descriptions";
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Arrival ComboBox
private void comboDeparture_DisplayMemberChanged(object sender, EventArgs e)
{
string connectionString = @"Base Schema Name=cyanair;data source=C:\Users\Client 0819\source\repos\Cyanair\cyanair.db";
SQLiteConnection conn = new SQLiteConnection(connectionString);
**String city = comboDeparture.DisplayMember;**
try
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT * FROM CyanairAirports WHERE Descriptions IS NOT '" + comboDeparture.SelectedValue.ToString() + "'";
richTextBox1.Text = "SELECT * FROM CyanairAirports WHERE Descriptions IS NOT '" + comboDeparture.SelectedValue + "'";
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
comboArrival.DataSource = dt;
comboArrival.ValueMember = "Descriptions";
comboArrival.DisplayMember = "Descriptions";
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
谢谢:)
您似乎正在处理 DisplayMemberChanged
event on comboDeparture
, and trying to update the values of comboArrival
in that handler. However, DisplayMemberChanged
only triggers when the DisplayMember
属性 更改。
DisplayMember
仅告诉控件在数据绑定控件上显示哪个 属性。它与 ComboBox
中选择的索引或值无关。因此,唯一一次填充 comboArrival
运行s 的代码是在您设置 comboDepartarture.DisplayMember
时的构造函数中。相反,处理 ComboBox.SelectedIndexChanged
or ComboBox.SelectedValueChanged
并设置 comboArrival
.
关于您的代码需要注意的其他一些重要事项。
首先,当 运行ning Sql 语句时应该使用参数化查询,而不是连接字符串。在您进行操作时连接字符串可以帮助您 SQL Injection Attacks. I'm not familiar with SqlLite and can't provide you with an example of how to modify your code, but perhaps this question。
其次,您不需要在每次更改 comboDeparture
中的选定值时重新运行 查询。只需将 comboArrival
的数据源添加为 Form
上的字段即可对其进行过滤。例如...
public partial class main : Form
{
// Your constructors...
private void comboDepartures_SelectedIndexChanged(object sender, EventArgs e)
{
if (_arrivalsDataSource == null)
{
_arrivalsDataSource = new System.Data.DataTable();
// Load _arrivalsDataSource from the database, basically how you're doing it now.
comboArrival.DataSource = _arrivalsDataSource.DefaultView;
comboArrival.DisplayMember = "Descriptions"
comboArribal.ValueMember = "Descriptions"
}
if (comboDeparture.SelectedIndex == -1)
{
_arrivalsDataSource.DefaultView.RowFilter = null; // Clear the filter.
}
else
{
// Set the filter.
_arrivalsDataSource.DefaultView.RowFilter = $"Description <> '{comboDeparture.SelectedValue}'";
}
}
private System.Data.DataTable _arrivalsDataSource = null;
}