private void Button1_Click(System.Object sender, System.EventArgs e) { for (int x = 0; x <= ListBox1.Items.Count - 1; x++) { if (ListBox1.GetSelected(x)) { int tempX = x; ((checkHideColumnHeaderCell)dgv.Columns[Array.FindIndex(dgv.Columns.Cast <DataGridViewColumn>().ToArray(), c => c.HeaderText.Trim().Equals(ListBox1.GetItemText(ListBox1.Items[tempX])))].HeaderCell).isChecked = true; } } cms.Close(); }