Adding a blank row to a combo box thats dataSource is already set

I have a combo box that has a list of customers.  that i fill with a dataSet  (i do this graphically).  

upon load, i fill the dataSet from the SQLDataAdapter and then set the comboBox.text = "" , because i want it to start out empty.

But when i say msgbox combobox.text, it returns the first row of the dataSet.

How do i add a blank row to the combobox? My idea was before filling the dataSet that is bound to the combo to comboBox.items.add("") but this doesnt work because the dataSource is already set.

So what to do?

Also, when I use a dropDownList, i can not set the .text = "", how can i do this, because i dont want to let the user choose a customer that is not in the table?

Thanks
tricks801Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DabasCommented:
Hi tricks801,
What about adding a blank row to dataSet.Tables(0)?

Dabas
tricks801Author Commented:
i do it all graphically, so before i sqlAdapter.fill(dataSet)  in the code i should add a new row, how exactly do i do this, what about the primary key, if i try to add a blank row, wont it get pissed that i didnt add a primary key?
DabasCommented:
tricks801,
Give this a try:

Dim dt as DataTable

sqlAdapter.Fill(dt)
Dim dr as DataRow = dt.NewRow
dr.Item() = ""
dt.Rows.Add(dr)

Now bind the combobox to the DataTable

Dabas
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

tricks801Author Commented:
had a problem with dr.item() = "" because no accessible item accepts this number of arguments
DabasCommented:
tricks801,
sorry, mistyped
dr.item(0) = ""

Dabas
tricks801Author Commented:
how do you bind it....
Me.ComboBox1.DataSource = Me.DS_CustNames
and then what????
tricks801Author Commented:
and how is this not gonna give me a null pointer exception, the combobox has the custid as the valuemember and the name as the display member
DabasCommented:
tricks:

Me.ComboBox1.DataSource = dt

The data source now points to the data table, which is a disconnected recordsource, therefore will not affect your existant data.
As to valuemember vs display member, you probably have to also set dr.item(1) = "".
Or even better use dr.item("FieldName") = "" for both the value and display
David H.H.LeeCommented:
tricks801 ,
This is the easiest way to add a blank value into your dropdownlist
try this:
        comboBox.DataSource=Me.DS_CustNames
        comboBox.DataTextField="customer"
        comboBox.DataValueField="customer"
        comboBox.DataBind()
 '<----------add blank value or add some text into it like "---Please select customer name ---"
       comboBox.items.insert(0,new listitem("",-1))
 
Regards
x_com
     
David H.H.LeeCommented:
My previous comments is for Web Forms. If you need to set it in Window Form, you can bind the datasource for dropdownlist and use
comboBox.Items Add("") after the databinding process.
tricks801Author Commented:
Dabas:

it highlights mysqlAdapter.Fill(dt) and says  Value cannot be null.

x_com:

.Databind() is not valid, but databindings is???
DabasCommented:
tricks801:
Sorry. I keep doing this mistake:

Dim dt as NEW DataTable


Dabas

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tricks801Author Commented:
well, this finally worked

 Dim dt As New DataTable

        Me.SDA_CustNames.Fill(dt)
        Dim dr As DataRow = dt.NewRow
        dr.Item(1) = ""
        dr.Item(0) = -1
        dt.Rows.InsertAt(dr, 0)
        Me.ComboBox1.DisplayMember = "name"
        Me.ComboBox1.ValueMember = "CustID"
        Me.ComboBox1.DataSource = dt



but there is now a blank entry which looks bad, or i could put "Choose Customer" but then that would be in the list! why cant they just do it like they do in MsAccess!?!?! its so easy, and now 8 line of code just to make the combo box be initially empty!?!?!?!?!  

i cant believe that professionals use this trash, and thats how they do it.  there must be a real way to do this. like a setting for the combo box. im done with this!

thanks for all the help dabas.
David H.H.LeeCommented:
tricks801 ,
My first comments is use to work in WEB form, not Window Form. In window forms, just notices that it same with Web form method.Try a look this, this method is simple :
eg:
       Me.SDA_CustNames.Fill(dt)
       Me.ComboBox1.DisplayMember = "name"
       Me.ComboBox1.ValueMember = "CustID"
       Me.ComboBox1.DataSource = dt
       Me.ComboBox1.items.insert(0,"--Choose Customer--")

Regards
x_com
DabasCommented:
tricks801,
> i cant believe that professionals use this trash, and thats how they do it.
Well, I for one do not.
(Agree with your other statements btw)

The 'professional' way to do IMHO, is NEVER to bind.
I never do. And it is so easy to manually fill in the combobox by looping through the rows of the table and just using the Add method manually.

The extra bonus is that you can add OBJECTS, not just strings. You can create a class that holds not just a value member and a data member, but more fields which might be useful later. In the class you override the ToString method to determine which one of your fields will be the one that shows up in the combo.

This is the way THIS professional does it.
Ah.. and by the way, by not having the combo bound, I can easily add the extra blank or "Choose Customer" without any of your side effects!

Dabas
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.