【问题标题】:Eliminating an option from a ComboBox (2), based on the input of the first ComboBox (1) c#根据第一个 ComboBox (1) c# 的输入,从 ComboBox (2) 中消除一个选项
【发布时间】:2020-05-22 16:36:00
【问题描述】:

我正在创建一个航空公司预订系统,我有 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);
        }
        }

谢谢:)

【问题讨论】:

  • 那么,问题是什么?您发布的代码是否有问题?如果是这样,什么不起作用?是否抛出异常? comboArrival 没有填充? comboArrival 不过滤?
  • 使用条件和comboBox1.SelectedIndex = comboBox2.SelectedIndex; then true 不提交申请
  • 只需对数据源使用过滤器,而不是每次都运行新查询。并且永远不会将数据连接到一个字符串中进行查询
  • @JoshuaRobinson comboArrival 正在填充,但它不会过滤结果。例如,如果在 comboDeparture 中选择了“London”,我希望 London 在 comboArrival 中不作为选项出现。两个组合框都使用同一个位置表。

标签: c# sql forms sqlite combobox


【解决方案1】:

您似乎正在处理comboDeparture 上的DisplayMemberChanged 事件,并尝试在该处理程序中更新comboArrival 的值。但是,DisplayMemberChanged 仅在 DisplayMember 属性更改时触发。

DisplayMember 只告诉控件在数据绑定控件上显示哪个属性。它与ComboBox 中选择的索引或值无关。因此,填充comboArrival 的代码只有在您设置comboDepartarture.DisplayMember 时在构造函数中运行。相反,处理ComboBox.SelectedIndexChangedComboBox.SelectedValueChanged 并设置comboArrival 的项目。

关于您的代码需要注意的其他一些重要事项。

首先,您应该在运行 Sql 语句时使用参数化查询,而不是连接字符串。在你做的时候连接字符串打开你到SQL Injection Attacks。我对 SqlLite 不熟悉,无法为您提供如何修改代码的示例,但也许 this question 可以提供帮助。

其次,您不必每次更改comboDeparture 中的选定值时都重新运行查询。只需在Form 上添加comboArrival 的数据源作为字段,即可对其进行过滤。比如……

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多