Solved

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

Posted on 2003-12-02
15
1,154 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:tricks801
  • 6
  • 6
  • 3
15 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 9863846
Hi tricks801,
What about adding a blank row to dataSet.Tables(0)?

Dabas
0
 

Author Comment

by:tricks801
ID: 9864282
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?
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9864310
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
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:tricks801
ID: 9864360
had a problem with dr.item() = "" because no accessible item accepts this number of arguments
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9864367
tricks801,
sorry, mistyped
dr.item(0) = ""

Dabas
0
 

Author Comment

by:tricks801
ID: 9864394
how do you bind it....
Me.ComboBox1.DataSource = Me.DS_CustNames
and then what????
0
 

Author Comment

by:tricks801
ID: 9864399
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
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9864435
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
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9864879
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
     
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9865124
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.
0
 

Author Comment

by:tricks801
ID: 9867907
Dabas:

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

x_com:

.Databind() is not valid, but databindings is???
0
 
LVL 27

Accepted Solution

by:
Dabas earned 500 total points
ID: 9869570
tricks801:
Sorry. I keep doing this mistake:

Dim dt as NEW DataTable


Dabas
0
 

Author Comment

by:tricks801
ID: 9871707
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.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9871953
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
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9871973
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
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question