منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : تعبئة CheckListBox من قاعدة البيانات حسب الإختيار من Combobox
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم ورحمة الله وبركاته 

هل أستطيع عرض كافة حقول جدول من قاعدة البيانات Sql إلى CheckListBox  بإستخدام كومبوبوكس بحيث يعرض حسب القيمة المحددة في الكومبوبوكس 
بارك الله فيكم
كود :
Imports System.Data.SqlClient

Public Class Form1
   ' Your database connection string
   Dim connectionString As String = "YourConnectionStringHere"

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       ' Populate the ComboBox with data on form load
       PopulateComboBox()
   End Sub

   Private Sub PopulateComboBox()
       ' Your SQL query to retrieve distinct values for the ComboBox
       Dim query As String = "SELECT DISTINCT YourColumnName FROM YourTableName"
       
       ' Create a SqlConnection
       Using connection As New SqlConnection(connectionString)
           ' Open the connection
           connection.Open()

           ' Create a SqlCommand
           Using command As New SqlCommand(query, connection)
               ' Create a SqlDataReader to read data
               Using reader As SqlDataReader = command.ExecuteReader()
                   ' Clear existing items in the ComboBox
                   comboBox1.Items.Clear()

                   ' Add items to the ComboBox
                   While reader.Read()
                       comboBox1.Items.Add(reader("YourColumnName").ToString())
                   End While
               End Using
           End Using
       End Using
   End Sub

   Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox1.SelectedIndexChanged
       ' Clear existing items in the CheckBoxList
       checkBoxList1.Items.Clear()

       ' Check if an item is selected in the ComboBox
       If comboBox1.SelectedItem IsNot Nothing Then
           ' Get the selected value from the ComboBox
           Dim selectedValue As String = comboBox1.SelectedItem.ToString()

           ' Call a method to populate the CheckBoxList based on the selected value
           PopulateCheckBoxList(selectedValue)
       End If
   End Sub

   Private Sub PopulateCheckBoxList(selectedValue As String)
       ' Your SQL query to retrieve values for the CheckBoxList based on the selected value
       Dim query As String = "SELECT YourCheckBoxListColumnName FROM YourTableName WHERE YourComboBoxColumnName = @SelectedValue"

       ' Create a SqlConnection
       Using connection As New SqlConnection(connectionString)
           ' Open the connection
           connection.Open()

           ' Create a SqlCommand with parameters
           Using command As New SqlCommand(query, connection)
               ' Add parameters
               command.Parameters.AddWithValue("@SelectedValue", selectedValue)

               ' Create a SqlDataReader to read data
               Using reader As SqlDataReader = command.ExecuteReader()
                   ' Add items to the CheckBoxList
                   While reader.Read()
                       checkBoxList1.Items.Add(reader("YourCheckBoxListColumnName").ToString())
                   End While
               End Using
           End Using
       End Using
   End Sub
End Class