Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Auto-Fill ComboBox Using C# - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home C# Auto-Fill ComboBox Using C#

Auto-Fill ComboBox Using C#

E-mail
(3 votes, average: 5.00 out of 5)
This is simple code of how you fill a combobox automatically in C#.
First, lets bind the combo with some data:


protected  void FillCombo()
        {
            try
            {
                String strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Power\\Desktop\\db.mdb";
                OleDbConnection conn = new OleDbConnection(strconn);
                conn.Open();
                DataSet ds = new DataSet();
                String strSQL;
                OleDbCommand cmd;
                OleDbDataAdapter da = new OleDbDataAdapter();

                // Fill dataset
                //   DataRow dr;
                DataTable dt;
                da.TableMappings.Add("Table", "Positions");
                strSQL = "Select * From Positions";
                cmd = new OleDbCommand(strSQL, conn);
                cmd.CommandType = CommandType.Text;
                da.SelectCommand = cmd;
                da.Fill(ds);
                da.Dispose();
                ds.AcceptChanges();
                dt = ds.Tables[0];
                ds.Dispose();
                conn.Close();

                //Fill Combo
                this.cbPositions.Text = "";
                this.cbPositions.Items.Clear();
                this.cbPositions.BeginUpdate();

                //Load Postisions into the ComBox Control
                foreach (DataRow dr in dt.Rows)
                {
                    this.cbPositions.Items.Add(dr["Position"].ToString());

  }
                this.cbPositions.EndUpdate();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


Add the KeyPressEventHandler to you Designer.cs file:
            // cbPositions
            //
            this.cbPositions.FormattingEnabled = true;
            this.cbPositions.Location = new System.Drawing.Point(55, 133);
            this.cbPositions.Name = "cbPositions";
            this.cbPositions.Size = new System.Drawing.Size(121, 21);
            this.cbPositions.TabIndex = 0;
            this.cbPositions.KeyPress += new System.Windows.Forms.KeyPressEventHandler(cbPositions_KeyPress);//add this line


Finally, add the cbPositions_KeyPress Event:
 
        private void cbPositions_KeyPress(object sender,KeyPressEventArgs e)
        {
            cbPositions.DroppedDown  =true;
            if (char.IsControl(e.KeyChar ))
            {
               return ;
            }
            string str = cbPositions.Text.Substring( 0,cbPositions.SelectionStart) + e.KeyChar;
            Int32 index=  cbPositions.FindStringExact(str);
            if ( index ==-1)
            {
                index = cbPositions.FindString(str);
            }
            this.cbPositions.SelectedIndex =index;
            this.cbPositions.SelectionStart =str.Length ;
            this.cbPositions.SelectionLength = this.cbPositions.Text.Length - this.cbPositions.SelectionStart;
            e.Handled =true;
        }

Happy Coding!