Solved

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

Posted on 2003-12-02
15
1,151 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
Comment Utility
Hi tricks801,
What about adding a blank row to dataSet.Tables(0)?

Dabas
0
 

Author Comment

by:tricks801
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:Dabas
Comment Utility
tricks801,
sorry, mistyped
dr.item(0) = ""

Dabas
0
 

Author Comment

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

Author Comment

by:tricks801
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
tricks801:
Sorry. I keep doing this mistake:

Dim dt as NEW DataTable


Dabas
0
 

Author Comment

by:tricks801
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now