Results 1 to 4 of 4

Thread: [Resolved] Add one line of text to combo box after binding to dataset

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    [Resolved] Add one line of text to combo box after binding to dataset

    I have the following code which binds the combo box to a dataset.
    VB Code:
    1. Dim cn As New SqlClient.SqlConnection()
    2.         Dim da As SqlClient.SqlDataAdapter
    3.         Dim ds As New DataSet()
    4.  
    5.         cn.ConnectionString = "Integrated Security=True;" & _
    6.         "Data Source=Brutus;Initial Catalog=Problem Tracking;" & _
    7.         "user id=user;password=pass;"
    8.         cn.Open()
    9.  
    10.         da = New SqlClient.SqlDataAdapter("Select [Store Number] From Stores Order " & _
    11.         "By [Store Number]", cn)
    12.         da.Fill(ds, "Stores")
    13.  
    14.         cmbStores.Items.Clear()
    15.  
    16.         cmbStores.DataSource = ds.Tables("Stores")
    17.         cmbStores.DisplayMember = "Store Number"
    At the top of this list in this combo box, I would like to add "All"

    I tried this to no avail.
    VB Code:
    1. cmbStores.Items.Insert("All", 0)
    I received an error about the combo box being set.

    Anyone have any ideas?

    Thanks!
    Last edited by indydavid32; Dec 10th, 2003 at 08:45 AM.
    David Wilhelm

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    If you have to keep the combobox bound, i am afraid you can not add an item to it in this way. If not, populate it from the dataset but do not bound it and then add another item.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I did populate it manualy but then I had to run it through a do while loop. I was trying to avoid that. No big deal though, it's not a large table.

    Thanks for the reply.
    David Wilhelm

  4. #4
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    you can't manually add items to a bound control.

    but you can still fill it from a database and also add items manually likes this
    Code:
    using System;
    using System.Data.SqlClient
    
    SqlConnection Conn = new SqlConnection(ConnectionString);
    SqlCommand Comm = new SqlCommand("SELECT field1 FROM tableName", Conn);
    SqlReader reader;
    
    try
    {
       Conn.Open();
       reader = Comm.ExecuteNonQuery();
    
       ComboBox1.Items.Add("All");
       
       while(reader.Read())
       {
           ComboBox1.Items.Add(reader["field1"]);
       }
    
       reader.Close();
       Conn.Close();
    }
    catch(Exception ex)
    {
       MessageBox.Show("Error Occurred: " + ex.Message());
    }
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width