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