JayBeeOH

LINQ-to-SQL ComboBox Demo

Oct 5th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.65 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   LINQ-to-SQL ComboBox Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Oct 2015
  16. '
  17. ' Description:    Demonstrate various methods to code LINQ-to-SQL ComboBox
  18. '                 initialization.
  19. '
  20. '                 Documentation is at:
  21. '                   App's Visual Basic .NET code is at http://pastebin.com/gdZBAVKg
  22. '-------------------------------------------------------------------------------------------
  23.  
  24. Public Class MainForm
  25.  
  26.     Private RegionBindingSource As New BindingSource
  27.  
  28.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  29.         Handles Me.Load
  30.  
  31.         ' Prevent event from firing until after the load of the ComboBox.
  32.         RemoveHandler RegionDescriptionComboBox.SelectedIndexChanged,
  33.             AddressOf RegionDescriptionComboBox_SelectedIndexChanged
  34.  
  35.         '' (1)-----------------------------------------------------------
  36.         '' LINQ-to-SQL using a BindingSource and sorting the BindingSource.
  37.         '' Preferred for a Master/Detail or Parent/Child table
  38.         '' relationship.
  39.         '' --------------------------------------------------------------
  40.         'Using db As New NorthwindDataContext
  41.         '    RegionBindingSource.DataSource = db.Regions
  42.         'End Using
  43.  
  44.         'RegionBindingSource.Sort = "RegionDescription"
  45.  
  46.         'With RegionDescriptionComboBox
  47.         '    .DisplayMember = "RegionDescription"
  48.         '    .ValueMember = "RegionID"
  49.         '    .DataSource = RegionBindingSource
  50.         '    .SelectedIndex = -1
  51.         'End With
  52.  
  53.         '' (2)-----------------------------------------------------------
  54.         '' LINQ-to-SQL using a BindingSource and sorting in the query.
  55.         '' Preferred for a Master/Detail or Parent/Child table
  56.         '' relationship.
  57.         '' --------------------------------------------------------------
  58.         'Using db As New NorthwindDataContext
  59.         '    Dim myQuery = From rec In db.Regions
  60.         '                  Order By rec.RegionDescription
  61.         '                  Select rec
  62.  
  63.         '    RegionBindingSource.DataSource = myQuery
  64.         'End Using
  65.  
  66.         'With RegionDescriptionComboBox
  67.         '    .DisplayMember = "RegionDescription"
  68.         '    .ValueMember = "RegionID"
  69.         '    .DataSource = RegionBindingSource
  70.         '    .SelectedIndex = -1
  71.         'End With
  72.  
  73.         '' (3)-----------------------------------------------------------
  74.         '' LINQ-to-SQL w/o sorting. No BindingSource.
  75.         '' --------------------------------------------------------------
  76.         'Using db As New NorthwindDataContext
  77.         '    With RegionDescriptionComboBox
  78.         '        .DisplayMember = "RegionDescription"
  79.         '        .ValueMember = "RegionID"
  80.         '        .DataSource = db.Regions
  81.         '        .SelectedIndex = -1
  82.         '    End With
  83.         'End Using
  84.  
  85.         '(4)------------------------------------------------------------
  86.         ' LINQ-to-SQL using sorting. No BindingSource.
  87.         ' --------------------------------------------------------------
  88.         Using db As New NorthwindDataContext
  89.             Dim myQuery = From rec In db.Regions
  90.                           Order By rec.RegionDescription
  91.                           Select rec
  92.  
  93.             With RegionDescriptionComboBox
  94.                 .DisplayMember = "RegionDescription"
  95.                 .ValueMember = "RegionID"
  96.                 .DataSource = myQuery
  97.                 .SelectedIndex = -1
  98.             End With
  99.         End Using
  100.  
  101.         AddHandler RegionDescriptionComboBox.SelectedIndexChanged,
  102.             AddressOf RegionDescriptionComboBox_SelectedIndexChanged
  103.     End Sub
  104.  
  105.     Private Sub RegionDescriptionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
  106.         Handles RegionDescriptionComboBox.SelectedIndexChanged
  107.  
  108.         RegionIDResult.Text = RegionDescriptionComboBox.SelectedValue.ToString
  109.     End Sub
  110. End Class
Advertisement
Add Comment
Please, Sign In to add comment