The rows are identified and compared via a primary key. You're asking if the set of rows contains a row with the primary key
selected
.
Also, you should never make changes to an iterator within the iteration. This will throw an error also.
Try this instead:
private void dropDownDemo_Load(object sender, EventArgs e)
{
dt.Columns.Add("Data");
dt.Rows.Add("None");
dt.Rows.Add("1st");
dt.Rows.Add("2nd");
dt.Rows.Add("3rd");
}
public void removeSelected(string selected)
{
var dtTemp = new DataTable;
dtTemp .Columns.Add("Data");
if (selected != "None")
{
foreach (DataRow row in dt.Rows)
{
if (!dt.Rows["Data"].Contains(selected))
{
dtTemp.Rows.Add(row)
}
}
}
dt = dtTemp;
}
Another way would be to add a
Break;
after you remove the row, but this is pretty clean