Remove Item from ComboBox based on another selection using C# and VB.Net in Windows Application

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to add all the items from one combobox1 to combobox2 except selected item in combobx1 using vb.net windows forms

Posted 3 months ago Modified on 3 months ago

Hi satabeach,

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;

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.DataSource = GetComboData();
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "EmployeeID";

        comboBox2.DataSource = GetComboData();
        comboBox2.DisplayMember = "Name";
        comboBox2.ValueMember = "EmployeeID";
    }

    public DataTable GetComboData()
    {
        string conString = @"Data Source=.;Initial Catalog=Northwind;UID=sa;PWD=pass@123;";
        string query = "SELECT EmployeeID,FirstName + ' ' + LastName [Name] FROM Employees";
        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["EmployeeID"] = 0;
                    dr["Name"] = "Select";
                    dt.Rows.InsertAt(dr, 0);

                    return dt;
                }
            }
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = GetComboData();
        if (comboBox1.SelectedIndex > 0)
        {
            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dt.Rows[i];
                if (dr["EmployeeID"].ToString() == comboBox1.SelectedValue.ToString())
                {
                    dr.Delete();
                }
            }
            dt.AcceptChanges();
        }
        comboBox2.DataSource = dt;
        comboBox2.DisplayMember = "Name";
        comboBox2.ValueMember = "EmployeeID";
    }
}

VB.Net

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        comboBox1.DataSource = GetComboData()
        comboBox1.DisplayMember = "Name"
        comboBox1.ValueMember = "EmployeeID"
        comboBox2.DataSource = GetComboData()
        comboBox2.DisplayMember = "Name"
        comboBox2.ValueMember = "EmployeeID"
    End Sub

    Public Function GetComboData() As DataTable
        Dim conString As String = "Data Source=.;Initial Catalog=Northwind;UID=sa;PWD=pass@123;"
        Dim query As String = "SELECT EmployeeID,FirstName + ' ' + LastName [Name] FROM Employees"
        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("EmployeeID") = 0
                    dr("Name") = "Select"
                    dt.Rows.InsertAt(dr, 0)
                    Return dt
                End Using
            End Using
        End Using
    End Function

    Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged
        Dim dt As DataTable = GetComboData()
        If comboBox1.SelectedIndex > 0 Then
            For i As Integer = dt.Rows.Count - 1 To 0 Step -1
                Dim dr As DataRow = dt.Rows(i)
                If dr("EmployeeID").ToString() = comboBox1.SelectedValue.ToString() Then
                    dr.Delete()
                End If
            Next
            dt.AcceptChanges()
        End If
        comboBox2.DataSource = dt
        comboBox2.DisplayMember = "Name"
        comboBox2.ValueMember = "EmployeeID"
    End Sub
End Class

Screenshot