0

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 :)

Ares
  • 29
  • 4
  • 1
    Well, the error message is quite clear, isn't it? You can either stop binding the cblist or remove the items from the datasource. If instead of removing items you want them to be readonly you may want to look [here](https://stackoverflow.com/questions/4368618/how-to-disable-a-checkbox-in-a-checkedlistbox), or, recommended change to ListView and read [this](https://stackoverflow.com/questions/5472172/how-do-you-disable-an-item-in-listview-control-in-net-3-5) – TaW Nov 19 '19 at 13:09

1 Answers1

0

I've found a solution to my question. If they are not allowed to be null in SQL I set them unchecked all the time.

checkedListBoxPartList.ItemCheck += (obj, args) =>
            {
                if (mOS_PackOSDataSet.PARTLIST.Columns[args.Index].AllowDBNull == true)
                {
                    dynamic testOne = checkedListBoxPartList.Items[args.Index];
                    var test = args.NewValue == CheckState.Checked ? true : false;
                    ((MainSoftwareForm)mainSoftware).dataGridViewPartLists.Columns[(string)testOne.Name].Visible = test;
                }
                else
                {
                    dynamic item = checkedListBoxPartList.Items[args.Index];
                    if (args.CurrentValue == CheckState.Unchecked) args.NewValue = CheckState.Unchecked;
                    var visibleIndeterminate = args.NewValue == CheckState.Unchecked ? false : false;
                    ((MainSoftwareForm)mainSoftware).dataGridViewPartLists.Columns[(string)item.Name].Visible = visibleIndeterminate;
                }


            };
Ares
  • 29
  • 4