Solved

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

Posted on 2003-12-02
15
1,153 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 61
Entity Framework 7 43
Need to start a web service from Visual Studio 2015 Pro 2 25
Formula for calculating ROI on training 6 30
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…

832 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