I have a parent class defined as following:
using System.Collections.Generic;
namespace Test
{
public class GeneralClass
{
public class Parent
{
public string Parent_Name { get; set; }
public List<Child> List_Child { get; set; } = new List<Child>();
}
public class Child
{
public string Child_Name { get; set; }
}
}
}
Please note that the Child_Name has the following format: Parent_Name + "-" + an integer number.
Then in the same Form, I create two DataGridView (dt1 and dt2). On the dt1, each row shows the Parent_Name and on the dt2 each row shows the Child_Name. Each parent can have multiple children (List).
Now I want to: - Delete a parent (a row) on dt1, it would also delete all the associated children in dt2 (but not the children of other parent).
So far, what I've done is
// Iteration over selected parents
foreach (DataGridViewRow row_dt1 in dt1.SelectedRows)
{
if (!row.IsNewRow)
{
// Find parent name of actual row
string parent_name = row_dt1.Cells[0].Value.ToString();
// Iteration over all rows of children
foreach (DataGridViewRow row_dt2 in dt2.Rows)
{
// Find child name
object val1 = row_dt2.Cells[0].Value;
// If child name starts with parent name, remove this child from the DataGridView (dt2)
if (val1 != null && val1.ToString().StartsWith(parent_name + "-"))
{
dt2.Rows.Remove(row_dt2);
}
}
// Now remove the parent from dt1
dt1.Rows.Remove(row_dt1);
}
}
It deleted the selected parent as expected but it only deleted the first child of this parent (but not the others). Where did I do wrong?
Thank you very much!