I'm developing a windows form
application that shows in checkboxList
all the columns I have in a datagridview
. First I'm setting checked
all the items that can be null
in SQL
. I want to delete
all other that are not checked
or make them readonly
. I have tried to cast
it as checkbox but it's not working. I have tried deleting an item from the checkboxList but I get the following error Items collection cannot be modified when the DataSource property is set.
Any ideas or hints?
foreach (DataGridViewColumn column in ((MainSoftwareForm)mainSoftware).dataGridViewImportParts.Columns)
{
var columns = ((MainSoftwareForm)mainSoftware).dataGridViewImportParts.Columns.Cast<DataGridViewColumn>()
.Select(x => new { x.Name, x.HeaderText }).ToList();
checkedListBoxIPColumns.DataSource = columns;
checkedListBoxIPColumns.ValueMember = "Name";
checkedListBoxIPColumns.DisplayMember = "HeaderText";
//Set initial check state based on columns visibility
for (int i = 0; i < checkedListBoxIPColumns.Items.Count; i++)
{
if(mOS_PackOSDataSet.PARTLIST.Columns[i].AllowDBNull==true)
{
dynamic item = checkedListBoxIPColumns.Items[i];
checkedListBoxIPColumns.SetItemChecked(i, true);
}
else
{
}
}
checkedListBoxIPColumns.ItemCheck += (obj, args) =>
{
dynamic item = checkedListBoxIPColumns.Items[args.Index];
var visible = args.NewValue == CheckState.Checked ? true : false;
((MainSoftwareForm)mainSoftware).dataGridViewImportParts.Columns[(string)item.Name].Visible = visible;
};
}
I'll be really happy to explain why, if you are going to give a negative vote :)