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.