0

There are two datasets ds and ds1. want to change backcolor of datalist if values of two datasets are matched. We are comparing two datasets but value not compared.

for (Int32 i = 0; i < ds.Tables[0].Rows.Count-1; i++)
{ 
  for (Int32 x = 0; x < ds1.Tables[0].Rows.Count-1; x++) 
  { 
    if (ds.Tables[0].Rows[i][0].ToString() == ds1.Tables[0].Rows[x][0].ToString()) 
    { 
      DataList1.ItemStyle.BackColor = System.Drawing.Color.Red; 
    }
    else 
    { 
      DataList1.ItemStyle.BackColor = System.Drawing.Color.Green; 
    } 
  } 
} 
kgzdev
  • 2,770
  • 2
  • 18
  • 35

2 Answers2

1

You should break loops after finding match, otherwise loops continue and changes color each time it matches values or not until both loops end. Here is answer how to break out of nested loops: Breaking out of a nested loop

Greg
  • 346
  • 1
  • 10
0

As mentioned @Greg, you really need to break a loop after matching was found.

Futhermore, if you need to scan all rows in datasets, change your loop definitions:

for (Int32 i = 0; i < ds.Tables[0].Rows.Count; i++)
{ 
  for (Int32 x = 0; x < ds1.Tables[0].Rows.Count; x++) 

In your code you will skip the last rows of the both datasets, because loop will end when its counter will be equal Count - 2.

Miamy
  • 2,162
  • 3
  • 15
  • 32