Help reducing loops with filter code The following code is very slow. I was wondering if you can help me speed it up.

private void filter(List&lt;CheckBox&gt; boxList)
        {
            // refLanguageTabs is a TabControl the Tabcount is 9
            for (int i = 0; i &lt; refLanguageTabs.TabCount; i++)
            {
                Control[] ctrls = refLanguageTabs.TabPages[i].Controls.Find(refLanguageTabs.TabPages[i].Name + "Grid", true);
                DataGridView dgv = ctrls[0] as DataGridView;

                // average row count is 3000 
                for (int j = 0; j &lt; dgv.RowCount; j++)
                {
                    for (int k = 0; k &lt; boxList.Count; k++)
                    {
                        if (dgv.Rows[j].Cells[1].Value.ToString() != boxList[k].Name.ToString())
                        {
                            dgv.Rows[j].Visible = false;
                        }
                    }
                }
                dgv.Refresh();
            }
        }
Some few thoughts without testing or anything else:


Cache the value of the cell in a local variable.
Replace for-loops if possible with foreach-loops.
The call of boxList[x].Name.ToString() is unnecessary, it is already a String.
break after setting the visibility to avoid unnecessary checks.




foreach(DataGridViewRow row in dgv.Rows)
{
    String cellValue = row.Cells[1].Value.ToString();
    foreach(CheckBox boxItem in boxList)
    {
        if(cellValue == boxItem.Name)
        {
            row.Visible = false;
            break;
        }
    }
}


On another sidenote, please use meaningful variable names, even in such stupid iterating loops.