Try the below one. Here replace age with col1 and age2 with col2.
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt32(dr["Age"]) < Convert.ToInt32(dr["Age2"]))
{
foreach (DataRow dr2 in dt.Rows)
{
if (Convert.ToInt32(dr2["Age2"]) == Convert.ToInt32(dr["Age"]))
{
foreach (DataRow dr3 in dt.Rows)
{
if (Convert.ToInt32(dr3["Age2"]) < Convert.ToInt32(dr["Age2"]))
dr3["Age2"] = Convert.ToInt32(dr["Age"]) + 1;
if (Convert.ToInt32(dr3["Age2"]) == Convert.ToInt32(dr["Age2"]))
dr3["Age2"] = Convert.ToInt32(dr["Age"]);
}
}
}
}
i tried with similar scenario of table. Below is the my table contents. It worked perfectly.
ew Student() { Name = "Jack", Age = 1, Age2 = 15,StudentId = 100 },
new Student() { Name = "Smith", Age = 2, Age2 = 2,StudentId = 101 },
new Student() { Name = "Smit", Age = 3,Age2 = 3, StudentId = 102 },
new Student() { Name = "Smit", Age = 2,Age2 = 4, StudentId = 102 }