Hi Akram.19,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Linq
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable dtSelected { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = GetComboData();
comboBox1.ValueMember = "CategoryID";
comboBox1.DisplayMember = "CategoryName";
comboBox2.DataSource = GetComboData();
comboBox2.ValueMember = "CategoryID";
comboBox2.DisplayMember = "CategoryName";
comboBox3.DataSource = GetComboData();
comboBox3.ValueMember = "CategoryID";
comboBox3.DisplayMember = "CategoryName";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > 0)
{
GetSelectedData();
DataTable dt = GetRowsAfterDelete();
comboBox2.DataSource = dt;
comboBox2.ValueMember = "CategoryID";
comboBox2.DisplayMember = "CategoryName";
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex > 0)
{
GetSelectedData();
DataTable dt = GetRowsAfterDelete();
comboBox3.DataSource = dt;
comboBox3.ValueMember = "CategoryID";
comboBox3.DisplayMember = "CategoryName";
}
}
public DataTable GetComboData()
{
string conString = "Server=.;Database=Northwind;UID=sa;PWD=pass@123;";
string query = "SELECT CategoryID,CategoryName FROM Categories";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
DataRow dr = dt.NewRow();
dr["CategoryID"] = 0;
dr["CategoryName"] = "Select";
dt.Rows.InsertAt(dr, 0);
return dt;
}
}
}
}
private DataTable GetRowsAfterDelete()
{
DataTable dt = GetComboData();
DataTable dtResult = dt.Copy();
foreach (DataRow row in dtSelected.Rows)
{
DataRow dr = dt.Select("CategoryID=" + row["CategoryID"]).FirstOrDefault();
if (dr != null)
{
dt.Rows.Remove(dr);
}
}
return dt;
}
private void GetSelectedData()
{
dtSelected = new DataTable();
dtSelected.Columns.Add("CategoryID");
dtSelected.Columns.Add("CategoryName");
dtSelected.Rows.Clear();
if (comboBox1.SelectedIndex > 0)
{
DataRowView drv1 = (DataRowView)comboBox1.SelectedItem;
dtSelected.Rows.Add((drv1.Row as DataRow).ItemArray);
}
if (comboBox2.SelectedIndex > 0)
{
DataRowView drv2 = (DataRowView)comboBox2.SelectedItem;
dtSelected.Rows.Add((drv2.Row as DataRow).ItemArray);
}
if (comboBox3.SelectedIndex > 0)
{
DataRowView drv3 = (DataRowView)comboBox3.SelectedItem;
dtSelected.Rows.Add((drv3.Row as DataRow).ItemArray);
}
}
}
VB.Net
Private Property dtSelected As DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
comboBox1.DataSource = GetComboData()
comboBox1.ValueMember = "CategoryID"
comboBox1.DisplayMember = "CategoryName"
comboBox2.DataSource = GetComboData()
comboBox2.ValueMember = "CategoryID"
comboBox2.DisplayMember = "CategoryName"
comboBox3.DataSource = GetComboData()
comboBox3.ValueMember = "CategoryID"
comboBox3.DisplayMember = "CategoryName"
End Sub
Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If comboBox1.SelectedIndex > 0 Then
GetSelectedData()
Dim dt As DataTable = GetRowsAfterDelete()
comboBox2.DataSource = dt
comboBox2.ValueMember = "CategoryID"
comboBox2.DisplayMember = "CategoryName"
End If
End Sub
Private Sub comboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If comboBox2.SelectedIndex > 0 Then
GetSelectedData()
Dim dt As DataTable = GetRowsAfterDelete()
comboBox3.DataSource = dt
comboBox3.ValueMember = "CategoryID"
comboBox3.DisplayMember = "CategoryName"
End If
End Sub
Public Function GetComboData() As DataTable
Dim conString As String = "Server=.;Database=Northwind;UID=sa;PWD=pass@123;"
Dim query As String = "SELECT CategoryID,CategoryName FROM Categories"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Dim dr As DataRow = dt.NewRow()
dr("CategoryID") = 0
dr("CategoryName") = "Select"
dt.Rows.InsertAt(dr, 0)
Return dt
End Using
End Using
End Using
End Function
Private Function GetRowsAfterDelete() As DataTable
Dim dt As DataTable = GetComboData()
Dim dtResult As DataTable = dt.Copy()
For Each row As DataRow In dtSelected.Rows
Dim dr As DataRow = dt.Select("CategoryID=" & row("CategoryID")).FirstOrDefault()
If dr IsNot Nothing Then
dt.Rows.Remove(dr)
End If
Next
Return dt
End Function
Private Sub GetSelectedData()
dtSelected = New DataTable()
dtSelected.Columns.Add("CategoryID")
dtSelected.Columns.Add("CategoryName")
dtSelected.Rows.Clear()
If comboBox1.SelectedIndex > 0 Then
Dim drv1 As DataRowView = CType(comboBox1.SelectedItem, DataRowView)
dtSelected.Rows.Add((TryCast(drv1.Row, DataRow)).ItemArray)
End If
If comboBox2.SelectedIndex > 0 Then
Dim drv2 As DataRowView = CType(comboBox2.SelectedItem, DataRowView)
dtSelected.Rows.Add((TryCast(drv2.Row, DataRow)).ItemArray)
End If
If comboBox3.SelectedIndex > 0 Then
Dim drv3 As DataRowView = CType(comboBox3.SelectedItem, DataRowView)
dtSelected.Rows.Add((TryCast(drv3.Row, DataRow)).ItemArray)
End If
End Sub
Screenshot