Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

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
0
tricks801
Asked:
tricks801
  • 6
  • 6
  • 3
1 Solution
 
DabasCommented:
Hi tricks801,
What about adding a blank row to dataSet.Tables(0)?

Dabas
0
 
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?
0
 
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
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

Dabas
0
 
tricks801Author Commented:
how do you bind it....
Me.ComboBox1.DataSource = Me.DS_CustNames
and then what????
0
 
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
0
 
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
0
 
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
     
0
 
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.
0
 
tricks801Author Commented:
Dabas:

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

x_com:

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

Dim dt as NEW DataTable


Dabas
0
 
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.
0
 
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
0
 
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
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now