The code below works
void selectColumns_Click(object s, EventArgs e)
{
ListBox lBx = getListBox();
for (int i = 0; i < lBx.Items.Count-1;i++ )
{
bool contained = lBx.SelectedItems.Contains(lBx.Items[i]);
dgView.Columns[lBx.Items[i].ToString()].Visible = contained;
}
}
but this does not work- throws error "List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change."
void selectColumns_Click(object s, EventArgs e)
{
ListBox lBx = getListBox();
foreach (var item in lBx.Items )
{
bool cont = lBx.SelectedItems.Contains(item);
dgView.Columns[item.toString()].Visible = cont;
}
}
Get ListBox method is below:
ListBox getListBox()
{
return columnsForm.Controls.OfType<ListBox>().First() as ListBox;
}
also the listbox is getting populated by clicking on a hideColumnsButton
void hideBtn_Click(object sender, EventArgs e)
{
getListBox().Items.Clear();
foreach(DataGridViewColumn col in dgView.Columns)
{
getListBox().Items.Add(col.Name);
}
columnsForm.ShowDialog();
}