Combo box cannot show datasource of table in dataset

G

Guest

Hi Experts,

I have 2 tables in a dataset "ds", "Order" and "Customer". Order table
stores Customer ID; I want to show Customer Code in Customer table instead of
CustID in the combo box on a form data-binded to "Order". I code as following
and it works for other text control but not for the combo (Navigation button
works):

Dim constr As String =
ConfigurationSettings.AppSettings("SqlConnection1.ConnectionString")
Dim conn As New SqlConnection(constr)
Dim customer_comm As New SqlCommand("select * from tblCustomer", conn)
Dim order_da As New SqlDataAdapter("select * from tblorder", conn)
Dim customer_da As New SqlDataAdapter("select * from tblCustomer", conn)

Dim customer_read As SqlDataReader
Dim ds As New DataSet
Dim bm As BindingManagerBase

Private Sub frmOrder3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
row = 0
If conn.State = ConnectionState.Closed Then conn.Open()
order_da.Fill(ds, "Order")
customer_da.Fill(ds, "Customer")


cbxCustomer.DataSource = ds
cbxCustomer.DisplayMember = "Customer.CustCode"
cbxCustomer.ValueMember = "Customer.CustID"

MsgBox(cbxCustomer.Items.Count, MsgBoxStyle.Information) 'it
shows 4

txtOrderID.DataBindings.Add("Text", ds, "Order.OrderID") 'works
cbxCustomer.DataBindings.Add("SelectedValue", ds, "Order.CustID")
txtPo_No.DataBindings.Add("Text", ds, "Order.po_no") 'works

bm = BindingContext(ds, "Order")

conn.Close()
If conn.State = ConnectionState.Closed Then conn.Open()
ds.Clear()
order_da.Fill(ds, "Order")
Catch ex As Exception
MsgBox("Error: " & ex.ToString, MsgBoxStyle.Exclamation)
End Try
End Sub

Private Sub btnNavNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNavNext.Click
bm.Position += 1
End Sub

Private Sub btnNavPrevious_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnNavPrevious.Click
bm.Position -= 1
End Sub

Thanks,

DAVIS_HK
 
T

Tom Krueger [MSFT]

The problem has to do with databinding to SelectedValue. For some reason,
binding to SelectedValue does not seem to work properly. It may work
correctly for how it was designed, I just don't know why it does not work.

Anyway, I don't think you want to bind to that anyway. It looks like you are
trying to use the binding to select the appropriate Customer, however,
remember that bind works going the other way as well. If you select a
different customer, you would set the CustID on the order. So I guess it is
a good think it doesn't work.

Here is some code that should work for you with a couple modifications.
Basically in your FormLoad, call BindMasterDetails or perform this in your
FormLoad, then when the order position changes, the OnOrderPositionChanged
will fire updating the customer dropdown selection.
private void BindMasterDetails() {

// On Order Position Changed
BindingManagerBase orderBinding = this.BindingContext[OrderTable];
orderBinding.PositionChanged += new EventHandler(OnOrderPositionChanged);

// Call the OrderPositionChanged event so
// the appropriate customer gets selected.
OnOrderPositionChanged(null, null);

}

private void OnOrderPositionChanged(object sender, EventArgs e) {


// Get the current Order row.
int rowIndex = this.BindingContext[OrderTable].Position;
DataRowView selectedRow = OrderTable.DefaultView[rowIndex];

cbxCustomer.SelectedValue = selectedRow["CustID"]

}


--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top