C #, Lotus clever manner to avoid using two nested foreach loop

Problem:
need to be listed on the drop-down box Item2 selection DataGridViewRow the judge to see if a configuration with data inside the database table columns Item1 match.
If two matching foreach loop, will lead to logic complexity and only easy to break that layer inside the outer circulation while ignoring break cycles caused by the bug.

Solution:
clever use List, the Item1 statistics configuration table to meet the conditions attached to the List, and then use the List Contains method to determine whether there is DataGridViewRow line Item2 column is equal to Item1 related items.

Code:

List<string> list = new List<string>();

DataTable dt = xxxx;
foreach (DataRow dr in dt.Rows)
{
    list.Add(dr["Item1"].ToString());
}

foreach (DataGridViewRow row in dgv.Rows)
{
    if (list.Contains(row.Cells["Item2"].EditedFormattedValue.ToString()))
    {
        strXX = row.Cells["XXXX"].EditedFormattedValue.ToString();
        break;
    }
}

 

Guess you like

Origin www.cnblogs.com/zhang502219048/p/11769241.html