منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
تعبئة CheckListBox من قاعدة البيانات حسب الإختيار من Combobox - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (https://vb4arb.com/vb)
+-- قسم : قسم لغة الفيجوال بيسك VB.NET (https://vb4arb.com/vb/forumdisplay.php?fid=182)
+--- قسم : قسم اسئلة VB.NET (https://vb4arb.com/vb/forumdisplay.php?fid=183)
+--- الموضوع : تعبئة CheckListBox من قاعدة البيانات حسب الإختيار من Combobox (/showthread.php?tid=47307)



تعبئة CheckListBox من قاعدة البيانات حسب الإختيار من Combobox - صالح عبدالله - 12-10-23

السلام عليكم ورحمة الله وبركاته 

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


RE: تعبئة CheckListBox من قاعدة البيانات حسب الإختيار من Combobox - aljzazy - 14-10-23

كود :
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