Hi smile,
Refer below sample.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, Country FROM Employees", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Country";
this.comboBox1.ValueMember = "EmployeeId";
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue.GetType() != typeof(DataRowView))
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, Country FROM Employees WHERE Country =@Country", con))
{
cmd.Parameters.AddWithValue("@Country", comboBox1.Text);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.checkedListBox1.DataSource = dt;
this.checkedListBox1.DisplayMember = "EmployeeId";
this.checkedListBox1.ValueMember = "EmployeeId";
}
}
}
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox1.CheckedItems.Count > 0)
{
string statList = "(";
foreach (object item in checkedListBox1.CheckedItems)
{
DataRowView row = item as DataRowView;
statList += "'" + row["EmployeeId"] + "',";
}
statList = statList.Substring(0, statList.Length - 1) + ")";
string query = "SELECT EmployeeId, Country FROM Employees WHERE EmployeeId in " + statList;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeId, Country FROM Employees", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
dataGridView1.DataSource = dt
Me.comboBox1.DataSource = dt
Me.comboBox1.DisplayMember = "Country"
Me.comboBox1.ValueMember = "EmployeeId"
End Using
End Using
End Using
End Sub
Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If comboBox1.SelectedValue.[GetType]() <> GetType(DataRowView) Then
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeId, Country FROM Employees WHERE Country =@Country", con)
cmd.Parameters.AddWithValue("@Country", comboBox1.Text)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.checkedListBox1.DataSource = dt
Me.checkedListBox1.DisplayMember = "EmployeeId"
Me.checkedListBox1.ValueMember = "EmployeeId"
End Using
End Using
End Using
End If
End Sub
Private Sub checkedListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If checkedListBox1.CheckedItems.Count > 0 Then
Dim statList As String = "("
For Each item As Object In checkedListBox1.CheckedItems
Dim row As DataRowView = TryCast(item, DataRowView)
statList += "'" & row("EmployeeId") & "',"
Next
statList = statList.Substring(0, statList.Length - 1) & ")"
Dim query As String = "SELECT EmployeeId, Country FROM Employees WHERE EmployeeId in " & statList
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End If
End Sub
Screenshot