Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2004

Tip: Looking for answers? Try searching our database.

Binding a table with an inserted row to a combo box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kyle Baley - 05 Oct 2004 17:51 GMT
I've reduced my problem to the following code snipped (using the Northwinds
database). It's in VB.NET with a single form containing a single combo box
and some other control that can receive focus. Place the code in the form's
Load event:

       Dim connection As SqlConnection = New SqlConnection( "Data
Source=IPBSFIXED3;Initial Catalog=Northwind;User ID=sa;" )
       Dim command As SqlCommand = New SqlCommand( "SELECT ProductID,
ProductName FROM Products", connection )
       Dim adapter As SqlDataAdapter
       Dim ds As DataSet
       ds = New DataSet( )
       adapter = New SqlDataAdapter( command )
       adapter.Fill(ds)

       Dim dt As DataTable = ds.Tables(0)
       Dim dr As DataRow = dt.NewRow( )
       dr("ProductID") = -1
       dr("ProductName") = "-- Not selected --"
       dt.Rows.InsertAt( dr, 0 )
       ComboBox1.DataSource = ds.Tables(0)
       ComboBox1.DisplayMember = "ProductName"
       ComboBox1.ValueMember = "ProductID"

       command = New SqlCommand( "select * from [Order Details] where
OrderID=10248 and productid=11", connection )
       adapter = New SqlDataAdapter( command )
       ds = New DataSet( )
       adapter.Fill( ds )
       myData = ds.Tables(0)

       ComboBox1.DataBindings.Add( "SelectedValue", myData, "ProductID" )

Essentially, I'm:
- retrieving data from the Products table
- Inserting a row at the beginning
- Binding to the combo box
- Retrieving a table with some order details
- Binding the "ProductID" column to the currently selected combo box item

The problem is when I select the first value ("-- Not selected --"), nothing
else can receive focus. I can't even close the application.

Hopefully it's related to this issue:
http://groups.google.com/groups?q=insertat&hl=en&lr=&ie=UTF-8&group=microsoft.pu
blic.dotnet.framework.windowsforms.databinding&selm=esknemGvCHA.2600%40TK2MSFTNG
P11&rnum=2


because that's a variation a similar problem I'm having (and the suggested
solution doesn't work).
Kyle Baley - 05 Oct 2004 19:01 GMT
Forgot to mention, when I bind the combo box to a copy of the table (i.e.
ComboBox1.DataSource = ds.Tables(0).Copy( ) ), it works fine.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.