Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

bind list box selection to a data grid

$
0
0

Hi Folks;

I have a list box which is populated with data using a filtered data view (just want the name of one column), and then allow users to select an entry from the list, click a corrosponding button, and with the button click event, opens another form and populates the form using a parameter (same one used to populate list box). The result set would be any/all columns returned based on the same parameter.

I have seperate click events for multiple buttons, which all perform the same basic action, with the exception that each button returns data from seperate tables. This works quite well, but...

I attempted to combine the seperate click events into one, and use case select to determine which button was clicked, and return the appropriate results.

The problem that occurs is after converting to a generic click event, the results only return the first row in the dataset, regardless of the list box selection.

I'm including a code snippet with the hopes that someone may be able to catch this issue for me!




PrivateSub AllFindButtons(ByVal sender as system.objectByVal e as System.e.System.EventArgs)Handles btnSuppliersFind.click, btnCustomersfind.click

'cast sender as button type.
Dim btn as Button = CType(sender, Button)
Dim btnName as String = btn.Name

'determine text fields to use.
Dim text as string = string.empty
Dim table as System.Data.DataTable = Nothing

'Which text box and data table filter to use.
Select Case btnName
   Case "btnSuppliersFind"
      text = txtSuppliersName.Text
      table =NorthwindDataSet.Suppliers
   Case "btnCustomersFind"
      txt = txtCustomerName.Text
      table = NorthwindDataSet.Customers
End Select

Dim filteredview as Data.DataView = New Data.DataView(table)

' this row filter generalized
      filteredView.RowFilter = "CompanyName Like '%" + text + "%'"

      Dim rowsFound As Int32 = filteredView.Count

      Select Case rowsFound
         Case 0  ' no records found
            MessageBox.Show( _
            "No matching records found", _
            "No records found", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Exclamation)
         Case 1
            'which form you show depends on the button name
            Select Case btnName
               Case "btnSuppliersFind"
                  frmSuppliers.CompanyNameParameter = _
                     filteredView.Item(0)("CompanyName")
                  frmSuppliers.Show()
               Case "btnCustomersFind"
                  frmCustomerDetails.CompanyNameParameter = _
                     filteredView.Item(0)("CompanyName")
                  frmCustomerDetails.Show()
            End Select
         Case Else
            dlgPickMatchingCompany.FilteredView = filteredView
            Dim result As DialogResult
            result = dlgPickMatchingCompany.ShowDialog()
            If result = DialogResult.OK Then
               Dim rowView As Data.DataRowView
               rowView = dlgPickMatchingCompany.lbMatching.SelectedItem
               Dim companyName As String = rowView.Row.Item("CompanyName")

               ' which form you show depends on the button name
               Select Case btnName
                  Case "btnSuppliersFind"
                     frmSuppliers.CompanyNameParameter = _
                        filteredView.Item(0)("CompanyName")
                     frmSuppliers.Show()
                  Case "btnCustomersFind"
                     frmCustomerDetails.CompanyNameParameter = _
                        filteredView.Item(0)("CompanyName")
                     frmCustomerDetails.Show()
               End Select
            End If
      End Select
   End Sub


 

Viewing all articles
Browse latest Browse all 2535

Trending Articles