// Check if we should rename ALL rows DataGridViewRowCollection rows; if (renameAll) { rows = dataGridView1.Rows; } else { foreach (DataGridViewRow row in dataGridView1.SelectedRows) { rows.Add(row); } } // Is this the only solution? if (renameAll) { DataGridViewRowCollection rows = dataGridView1.Rows; } else { DataGridViewRowCollection rows; foreach (DataGridViewRow row in dataGridView1.SelectedRows) { rows.Add(row); } }