Combobox表现异常 [英] Combobox is Behaving Abnormally

查看:65
本文介绍了Combobox表现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winForm应用程序中有多个组合框。我正在从我的数据库填充这些组合框,我也启用了自动完成模式。问题是,当我搜索任何项目然后选择搜索的项目时,组合框不会选择
我的选择它从列表中选择任何随机索引。



任何帮助都会受到高度赞赏,因为我在这个问题上遇到了这么多天。



I have multiple combobox in my winForm application. I am populating those comboboxes from my database and I have enabled the autocompletemode also. The problem is that when I search any item and then selects the searched item, the combobox doesn't selects my selection it selects any random index from the list.

Any help would be highly appreciated because I am stuck in this problem for so many days.

  private void Expected_Load(object sender, EventArgs e)
        {
            //this.Activated += new EventHandler(ReloadExpectedForm);
            
            LoadExpected();
            box_imp_exp.SelectedIndex = 0;
        }

public void LoadExpected()
        {
            imp_exp.SelectedIndex = 0;
            this.AcceptButton = btnExpectedSave;
            this.CancelButton = btnExpectedCancel;
            id_expected.KeyDown += new KeyEventHandler(id_expected_KEYDOWN);
        

            arrivalDate_expected.CustomFormat = "MM/dd/yy";
            arrivalDate_expected.Format = DateTimePickerFormat.Custom;
            //arrivalDate_expected.ShowUpDown = true;

            departureDate_expected.CustomFormat = "MM/dd/yy";
            departureDate_expected.Format = DateTimePickerFormat.Custom;
            //departureDate_expected.ShowUpDown = true;


            estimatedTime_expected.CustomFormat = "hh:mm tt";
            estimatedTime_expected.Format = DateTimePickerFormat.Custom;
            estimatedTime_expected.ShowUpDown = true;

          
            GetFormData();
        }
    public void GetFormData()
        {

            List<string> crafts = new List<string>();

            OracleConnection conn = new OracleConnection(Data.connString_ORA);
            OracleCommand comm = new OracleCommand();

            comm.Connection = conn;
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                comm.CommandText = "SELECT ct_sht_desc from cargo_type";
                OracleDataReader reader = comm.ExecuteReader();
                if (reader.HasRows)
                {

                    int i = 1;
                    crafts.Insert(0, "Select Commodity");
                    while (reader.Read())
                    {
                        crafts.Insert(i, reader["ct_sht_desc"].ToString());
                        i++;
                    }

                }
                else
                {
                    crafts.Insert(0, "No Commodity Found");
                }



                commodity_expected.DataSource = crafts;
                commodity_expected.SelectedIndex = 0;
                commodity_expected.BindingContext = new System.Windows.Forms.BindingContext();
                
                crafts.Clear();
                reader.Close();

                comm.CommandText = "SELECT unit_sht_desc FROM units";
                reader = comm.ExecuteReader();
                if (reader.HasRows)
                {

                    int i = 1;
                    crafts.Insert(0, "Select Unit");
                    while (reader.Read())
                    {
                        crafts.Insert(i, reader["unit_sht_desc"].ToString());
                        i++;
                    }

                }
                else
                {
                    crafts.Insert(0, "No Unit Found");
                }



                cargo_unit.DataSource = crafts;
                cargo_unit.SelectedIndex = 0;
                cargo_unit.BindingContext = new System.Windows.Forms.BindingContext();

                crafts.Clear();
                reader.Close();

                comm.CommandText = "SELECT p_desc FROM port";
                reader = comm.ExecuteReader();
                if (reader.HasRows)
                {

                    int i = 1;
                    crafts.Insert(0, "Select Port");
                    while (reader.Read())
                    {
                        if (reader["p_desc"].ToString() != "No Port")
                        {
                            crafts.Insert(i, reader["p_desc"].ToString());
                            i++;
                        }
                    }

                }
                else
                {
                    crafts.Insert(0, "No Port Found");
                }



                lastport_expected.DataSource = crafts;
                lastport_expected.SelectedIndex = 0;
                lastport_expected.BindingContext = new System.Windows.Forms.BindingContext();

                crafts.Clear();
                reader.Close();
                SqlConnection sqlconn = new SqlConnection(Data.connString);
                SqlCommand sqlcomm = new SqlCommand();
                id = 0;
                
                if (sqlconn.State==ConnectionState.Closed)
                {
                    sqlconn.Open();
                }
                sqlcomm.Connection = sqlconn;
                sqlcomm.CommandText = @"SELECT MAX(id) FROM shipping ;";
                SqlDataReader sqlreader = sqlcomm.ExecuteReader();
                if (sqlreader.HasRows)
                {
                    sqlreader.Read();
                    id = Convert.ToInt32(sqlreader[0].ToString());
                    id++;

                }
                else
                {
                    id = 0;
                }

                id_expected.Text = id.ToString();
                sqlreader.Close();

            }
            catch (Exception ex)
            {
                conn.Close();
                MessageBox.Show("Somme Error Occured while fetching information\nError\n" + ex);
            }

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                comm.CommandText = "SELECT s_name from ship";
                OracleDataReader reader = comm.ExecuteReader();
                if (reader.HasRows)
                {

                    int i = 1;
                    crafts.Insert(0, "Select Ship");
                    while (reader.Read())
                    {
                        crafts.Insert(i, reader["s_name"].ToString());
                        i++;
                    }

                }
                else
                {
                    crafts.Insert(0, "No Ship Found");
                }


                shipname_expected.DataSource = crafts;

                shipname_expected.BindingContext = new System.Windows.Forms.BindingContext();
                crafts.Clear();
                reader.Close();
               
                comm.CommandText = "SELECT ag_desc from shipping_agency";
                reader = comm.ExecuteReader();
                if (reader.HasRows)
                {

                    int i = 1;
                    crafts.Insert(0, "Select Agent");
                    while (reader.Read())
                    {
                        crafts.Insert(i, reader["ag_desc"].ToString());
                        i++;
                    }

                }
                else
                {
                    crafts.Insert(0, "No Agent Found");
                }

                agentname_expected.DataSource = crafts;
                agentname_expected.SelectedIndex = 0;
                agentname_expected.BindingContext = new System.Windows.Forms.BindingContext();
                conn.Close();



            }
            catch (Exception ex)
            {
                conn.Close();
                MessageBox.Show("Somme Error Occured\nError\n" + ex);
            }

        }




$







推荐答案

我的winForm应用程序中有多个组合框。我正在从我的数据库填充这些组合框,我也启用了自动完成模式。问题是,当我搜索任何项目然后选择搜索的项目时,组合框不会
选择我的选择它从列表中选择任何随机索引。



任何帮助都会受到高度赞赏,因为我在这个问题上遇到了这么多天。







I have multiple combobox in my winForm application. I am populating those comboboxes from my database and I have enabled the autocompletemode also. The problem is that when I search any item and then selects the searched item, the combobox doesn't selects my selection it selects any random index from the list.

Any help would be highly appreciated because I am stuck in this problem for so many days.



我的Stackoverflow Post的代码更多地解释了这个问题,请看看


The question is more explained with code at my Stackoverflow Post kindly take a look

http://stackoverflow.com/questions/40758011/combobox-behaving-abnormally

http://stackoverflow.com/questions/40758011/combobox-behaving-abnormally


这篇关于Combobox表现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆