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.

How to chage the source of a datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Douglas Buchanan - 04 Oct 2004 04:12 GMT
Sqls2k / vb.net winforms / ado.net / newbie

This code loads a Datagrid from a customer id supplied by a combo box.
It only works the first time I open the form. I will correctly get the
customer I select in the combo box, but it will not get any subsequent
customers I select in the combo box. Why?

I have used a message box to verify that the combo box receives the
proper id's, It does. The problem must be with something I don't
understand about using stored procedures or about this code.

====== problem code =======
Private Sub LoadOrderGrid()

 SqlSelectCommand1.Parameters("@CustomerID").Value =
cboCustomers.SelectedValue
       
 SqlDataAdapter1.Fill(DsOrders1, "Orders")

 OrderGrid.DataSource = DsOrders1.Tables("Orders").DefaultView

   End Sub
===========================
james - 04 Oct 2004 06:15 GMT
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

> Sqls2k / vb.net winforms / ado.net / newbie
>
[quoted text clipped - 19 lines]
>    End Sub
> ===========================
Douglas Buchanan - 04 Oct 2004 14:27 GMT
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]
>
> ======================================

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.