VB.NET•Use a DataRow Array and DataView to Display Selected Line Items

Listing 3. When the user makes a selection in the Product Categories dropdown list (see Figure 2), the cboProdCats_SelectedIndexChanged event handler creates a DataRow array to populate the "Products in Category" list. The strSelect criterion also generates a DataView that binds to the DataGrid. A cboProducts_SelectedIndexChanged event handler (not shown here) selects the DataGrid row for processing order line items.

Private Sub cboProdCats_SelectedIndexChanged( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles cboProdCats.SelectedIndexChanged
   'Add selected items to Products in Category 
   'combo box
   'Create a DataView and bind the DataGrid to it
   Try
      dgProds.CaptionText = "Products in " + _
         cboProdCats.SelectedItem.ToString _
         + " Category"
      Dim strSelect As String = "catName = '" + _
         cboProdCats.SelectedItem.ToString _
         + "'"
      Dim datRow() As DataRow = _
         dsProds.Tables(0).Select(strSelect)
      Dim intCtr As Integer
      With cboProducts
         .Items.Clear()
         For intCtr = 0 To UBound(datRow)
            'Populate the Products 
            'combo box
            .Items.Add(datRow(intCtr) _
            (3).ToString + ": " + _
            datRow(intCtr)(4).ToString + _
            " (" + datRow(intCtr)(2) _
            .ToString + ")")
         Next intCtr
         If .Items.Count > 0 Then
            'Add a Products DataView
            dvProds = New DataView
            dvProds = _
               dsProds.DefaultViewManager _
               .CreateDataView(dsProds.Tables(0))
            dvProds.RowFilter = strSelect
            dvProds.Sort = "model"
            Dim intRows As Integer = _
               dvProds.Count
            'Add TableStyle and 
            'ColumnStyle mapping names
            'Styles are persisted at 
            'design time 
            dgProds.DataSource = dvProds
            dgProds.AllowNavigation = _
               False
            dgProds.Visible = True
            dgProds.TableStyles(0) _
               .MappingName = _
               dsProds.Tables(0) _
               .TableName
            Dim colStyle As _
               DataGridColumnStyle
            For intCtr = 0 To _
               dsProds.Tables(0) _
               .Columns.Count - 1
               colStyle = _
                  dgProds.TableStyles(0) _
               .GridColumnStyles(intCtr)
               colStyle.MappingName _
                  = dsProds.Tables _
                  (0). _
                  Columns(intCtr) _
                  .ColumnName
            Next intCtr
         Else
            'No products, hide the 
            'DataGrid
            .Items.Add( _
               "No products in category")
            dgProds.Visible = False
         End If
         .SelectedIndex = 0
      End With
   Catch excView As Exception
      MsgBox(excView.Message, _
         MsgBoxStyle.Critical, _
         "Exception Creating DataView")
   End Try
End Sub