Avoid adding duplicate datarows
.net, c#, datagridview, duplicates, winforms
Solution
Check if the row already exists in the `Data Table` by using the `.Find` method:
var rowExists = dt.Rows.Find(dr);
if (rowExists == null) { // The row doesn't exist
dt.Rows.Add(dr); // Add the row
}
Problem
In my Windows Forms Application, basically I have two `DataGridView`s. I want to select rows from right one and add selected rows to the left. The right `DataGridView`has a checkbox column. See the image : For example : Click the first row and Click Add button. Click the third row and Click Add button. The problem is the first row was added twice because it was ticked. How can I make sure that rows added are distinct? My Code: ``` foreach (DataGridViewRow row in dgRight.Rows) { DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell; if (check.Value != null) { if ((bool)check.Value) { DataRow myRow = (row.DataBoundItem as DataRowView).Row; DataRow dr = dt.NewRow(); dr[0] = myRow[0]; dr[1] = myRow[1]; dr[2] = myRow[2]; dr[3] = myRow[3]; dt.Rows.Add(dr); } } } dgLeft.DataSource = dt; ```