Prevent duplicate record Insert in DataGridView using C# and VB.Net in Windows Application

Last Reply 2 months ago By dharmendr

Posted 2 months ago

prevent adding duplicate data into datagridview 

Using this code I am getting the following result, it stops adding after the orange

Mango

Apple

Papay

Orange

Expected Output

Mango

Apple

Papay

Orange

Banana

Pineapple

Strawberry

Blueberry

Dim datatable As DataTable = New DataTable
datatable.Columns.Add("Items")
datatable.Rows.Add("Mango")
datatable.Rows.Add("Apple")
datatable.Rows.Add("Papaya")
datatable.Rows.Add("Orange")
datatable.Rows.Add("Banana")
datatable.Rows.Add("Pineapple")
datatable.Rows.Add("Strawberry")
datatable.Rows.Add("Blueberry")
Me.Combobox1.datasource = datatable

Dim Found As Boolean = False
If datatable IsNot Nothing AndAlso datatable.Rows.Count > 0 Then
    For i = 0 To datatable.Rows.Count - 1
        For j = 0 To Me.DataGridView2.Rows.Count - 1
            If Convert.ToString(Me.DataGridView2.Rows(j).Cells(1).Value) = Combobox1.SelectedItem Then ' Selected Item = Orange
                Found = True
                'Exit For
            End If
        Next
        If Not Found Then
            Me.DataGridView2.Rows.Add(datatable.Rows(i)(0).ToString())
        End If
    Next
End If

 

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago

Hi satabeach,

Check this example. Now please take its reference and correct your code.

Code

C#

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

    private DataTable datatable = new DataTable();
    private DataTable dt1 = new DataTable();
    private void Form1_Load(object sender, EventArgs e)
    {
        datatable.Columns.Add("Items");
        datatable.Rows.Add("Mango");
        datatable.Rows.Add("Apple");
        datatable.Rows.Add("Papaya");
        datatable.Rows.Add("Orange");
        datatable.Rows.Add("Banana");
        datatable.Rows.Add("Pineapple");
        datatable.Rows.Add("Strawberry");
        datatable.Rows.Add("Blueberry");
        this.ComboBox1.DataSource = datatable;
        this.ComboBox1.ValueMember = "Items";
        this.ComboBox1.DisplayMember = "Items";


        dt1.Columns.Add("Items");
        dt1.Rows.Add("Mango");
        dt1.Rows.Add("Apple");
        dt1.Rows.Add("Papaya");
        dt1.Rows.Add("Orange");

        this.DataGridView1.DataSource = dt1;
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        bool Found = false;
        for (var j = 0; j <= this.DataGridView1.Rows.Count - 1; j++)
        {
            if (Convert.ToString(this.DataGridView1.Rows[j].Cells[0].Value) == ComboBox1.SelectedValue.ToString())
            {
                Found = true;
                break;
            }
        }

        if (!Found)
        {
            dt1.Rows.Add(ComboBox1.SelectedValue);
            this.DataGridView1.DataSource = dt1;
        }
    }
}

VB.Net

Public Class Form1
    Dim datatable As DataTable = New DataTable
    Dim dt1 As DataTable = New DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        datatable.Columns.Add("Items")
        datatable.Rows.Add("Mango")
        datatable.Rows.Add("Apple")
        datatable.Rows.Add("Papaya")
        datatable.Rows.Add("Orange")
        datatable.Rows.Add("Banana")
        datatable.Rows.Add("Pineapple")
        datatable.Rows.Add("Strawberry")
        datatable.Rows.Add("Blueberry")
        Me.ComboBox1.DataSource = datatable
        Me.ComboBox1.ValueMember = "Items"
        Me.ComboBox1.DisplayMember = "Items"


        dt1.Columns.Add("Items")
        dt1.Rows.Add("Mango")
        dt1.Rows.Add("Apple")
        dt1.Rows.Add("Papaya")
        dt1.Rows.Add("Orange")

        Me.DataGridView1.DataSource = dt1
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim Found As Boolean = False
        For j = 0 To Me.DataGridView1.Rows.Count - 1
            If Convert.ToString(Me.DataGridView1.Rows(j).Cells(0).Value) = ComboBox1.SelectedValue.ToString() Then
                Found = True
                Exit For
            End If
        Next

        If Not Found Then
            dt1.Rows.Add(ComboBox1.SelectedValue)
            Me.DataGridView1.DataSource = dt1
        End If
    End Sub
End Class

Screenshot