You can move your code to the ComboBox's SelectedItemChanged event.
Something like this:
Private Sub cboCustomers_SelectedValueChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles cboCustomers.SelectedValueChanged
SqlSelectCommand1.Parameters("@CustomerID").Value =
cboCustomers.SelectedValue
SqlDataAdapter1.Fill(DsOrders1, "Orders")
OrderGrid.DataSource = DsOrders1.Tables("Orders").DefaultView
End Sub
Or if you want to keep the code in LoadOrderGrid you can just do this:
Private Sub cboCustomers_SelectedValueChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles cboCustomers.SelectedValueChanged
LoadOrderGrid()
End Sub
That should work.(it works in a simple example I made up to be sure I was
giving you the right info.)
james
That does not seem to be the problem. Please read on.
Remember, the code works for any customer I select, but only the first
time.
1.) Currently I have the LoadOrderGrid() call in
cboCustomers_SelectionChangeCommitted ~ this works as described.
2.) Before that I had the LoadOrderGrid() call a button on the form
btnLoadOrders_Click ~ this works as described.
3.) I tried your suggestion and put the call in
cboCustomers_SelectedValueChanged ~ this returns the
'System.InvalidCastException' error before the form opens. The error
occurs on the following statement:
SqlDataAdapter1.Fill(DsOrders1, "Orders")
======= my code =======
Private Sub LoadOrderGrid()
'Supply the Parameter to the stored procedure for the OrderID
SqlSelectCommand1.Parameters("@CustomerID").Value =
cboCustomers.SelectedValue
'Fill the DataSet
SqlDataAdapter1.Fill(DsOrders1, "Orders")
'Populate the DataGrid by supplying the DataSource
OrderGrid.DataSource = DsOrders1.Tables("Orders").DefaultView
End Sub
Private Sub LoadOrderDetails()
Dim intOrderID As Integer 'OrderID
Dim intOrderOrdinalColumn As Integer 'Column
Dim intOrderOrdinalRow As Integer 'Row
Dim cellOrder As DataGridCell 'Cell
cellOrder = OrderGrid.CurrentCell 'Current order cell
'Clear the OrderDetail DataSet & DataGrid
DsOrderDetail1.Clear()
OrderDetailsGrid.Refresh()
'Determine the column
'0 indicates the column where the OrderID is stored
intOrderOrdinalColumn = 0
'Determine the row
intOrderOrdinalRow = cellOrder.RowNumber
'Gets the value of the cell at the specified the row and column
(returns Object)
intOrderID = CInt(OrderGrid(intOrderOrdinalRow,
intOrderOrdinalColumn))
'Supply the Parameter to the stored procedure for the OrderID
SqlSelectCommand2.Parameters("@OrderID").Value = intOrderID
'Fill the DataSet
SqlDataAdapter2.Fill(DsOrderDetail1, "OrderDetails")
'Populate the DataGrid by supplying the DataSource
OrderDetailsGrid.DataSource =
DsOrderDetail1.Tables("OrderDetails").DefaultView
End Sub
Private m_intRowNumber As Integer
Private Sub OrderGrid_CurrentCellChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles OrderGrid.CurrentCellChanged
'get the last row number
Dim intRowNumber As Integer = m_intRowNumber
'get the current row number
m_intRowNumber = (OrderGrid.CurrentCell.RowNumber())
'compare last with current to see if there was a change in row
selection
If m_intRowNumber <> intRowNumber Then
LoadOrderDetails()
End If
End Sub
Private Sub NWShoppingCartControl_Load(ByVal sender As Object, ByVal
e As System.EventArgs) Handles MyBase.Load
'Fill the DataSet
SqlDataAdapter3.Fill(DsCustomers1, "Customers")
'Supply the DataSource to the Control
cboCustomers.DataSource = DsCustomers1.Tables("Customers")
'Specify the DisplayMember & ValueMember
cboCustomers.DisplayMember = "CompanyName"
cboCustomers.ValueMember = "CustomerID"
'Run the Code to Fill the DataGrid
' LoadOrderGrid()
End Sub
Private Sub cboCustomers_SelectionChangeCommitted(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
cboCustomers.SelectionChangeCommitted
'Run the Code to Fill the DataGrid
' LoadOrderGrid()
End Sub
Private Sub btnLoadOrders_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnLoadOrders.Click
'Run the Code to Fill the DataGrid
' LoadOrderGrid()
End Sub
Private Sub cboCustomers_SelectedValueChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
cboCustomers.SelectedValueChanged
'Run the Code to Fill the DataGrid
LoadOrderGrid()
End Sub
======================================
james - 04 Oct 2004 19:49 GMT
Douglas, sorry that did not help. It appears to me that the changed value
in:
'Supply the Parameter to the stored procedure for the OrderID
SqlSelectCommand1.Parameters("@CustomerID").Value =
cboCustomers.SelectedValue
is not being changed when you make later selections. The
cboCustomers.SelectedValue does not change when you make a different
selection. what does change is the .SelectedItem or .SelectedText.
You could convert one of those to the correct type for your Sql statement by
converting .SelectedItem or .SelectedText to an Integer and passing that
to your SQL statement.
Sorry if that does not work. If I had the actual program or at least a
sample that reproduces the problem, I might be able to help more.
Hopefully, someone here will see the problem and offer a working solution.
james
> That does not seem to be the problem. Please read on.
>
[quoted text clipped - 123 lines]
>
> ======================================