|
-
Dec 5th, 2003, 04:32 PM
#1
Thread Starter
Fanatic Member
[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:
Dim cn As New SqlClient.SqlConnection()
Dim da As SqlClient.SqlDataAdapter
Dim ds As New DataSet()
cn.ConnectionString = "Integrated Security=True;" & _
"Data Source=Brutus;Initial Catalog=Problem Tracking;" & _
"user id=user;password=pass;"
cn.Open()
da = New SqlClient.SqlDataAdapter("Select [Store Number] From Stores Order " & _
"By [Store Number]", cn)
da.Fill(ds, "Stores")
cmbStores.Items.Clear()
cmbStores.DataSource = ds.Tables("Stores")
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:
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
-
Dec 5th, 2003, 04:36 PM
#2
Frenzied Member
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
-
Dec 5th, 2003, 04:37 PM
#3
Thread Starter
Fanatic Member
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.
-
Dec 5th, 2003, 04:41 PM
#4
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|