如何通过考虑 C# 中的列状态来读取 .csv 文件并启用或禁用复选框

How to read .csv file and enable or disable the checkbox by considering column status in C#

我有一个如下所示的 .csv 文件;

Name,Age,Marks0,Marks 1,Marks2,Marks3
Amal,22,TRUE,FALSE,FALSE,FALSE
Nimal,30,TRUE,TRUE,FALSE,FALSE
Perera,19,TRUE,TRUE,FALSE,FALSE
Sunil,25,TRUE,TRUE,FALSE,FALSE
Amali,22,TRUE,TRUE,FALSE,FALSE
Ann,26,TRUE,TRUE,FALSE,FALSE
Chamath,27,TRUE,FALSE,FALSE,TRUE
Kalana,29,TRUE,FALSE,FALSE,TRUE
Tom,25,TRUE,FALSE,FALSE,TRUE
Jerry,22,TRUE,FALSE,FALSE,TRUE
Peter,23,TRUE,FALSE,FALSE,TRUE

所以,我想读取这个.csv 文件并检查Marks0、Marks1、Marks2、Marks3 的状态。之后,我想在button1点击浏览这个file.my界面时启用或禁用checkbox是这样的。 Interface of my code

在这里要检查Marks0、Marks1、Marks2、Marks3的状态 条件是这样的,选定的列的整个数据都是 TRUE 然后启用 checkBox.but 那里有 True 和 False 然后 checkBox 是 enabled.but 都是 False 然后 checkBox 是 Disable.

这里有一个例子

Marks0 都为 TRUE 然后 checkBox1 启用

Marks1 全部为 TRUE 和 FALSE 然后 checkBox2 启用

Marks2 全部为 FALSE 然后 checkBox3 被禁用

Marks3 都是 TRUE 和 FALSE 然后启用 checkBox4 就这样,我想构建我的代码。

我可以启用复选框整列为 TRUE,我可以禁用复选框整列为 FALSE,但我无法像 Marks1 那样启用复选框为 TRUE 和 False,Marks3.please 给我一个解决方案这个。

我的代码如下;

  private void button1_Click(object sender, EventArgs e)
    {
        ofd.Filter = "*.csv|*.csv";
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //"C:\BA2000";
        fileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            tbOutputFilePath.Text = ofd.FileName;
            fileOriginalOutputPath = tbOutputFilePath.Text;


            if (tbOutputFilePath != null)
            {

                List<Marks> ObservingData = new List<Marks>(); // List to store all available Marks objects from the CSV
                Marks statusInt = new Marks();
                // Loops through each lines in the CSV
                foreach (string line in System.IO.File.ReadAllLines(tbOutputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
                {
                    // here line stands for each line in the CSV file

                    string[] InCsvLine = line.Split(',');

                    statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
                    statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
                    statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
                    statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);

                }

                if (statusInt.Mark0 == false)
                {
                    checkBox1.Enabled = false;
                }

                if (statusInt.Mark1 == false)
                {
                    checkBox2.Enabled = false;
                }

                if (statusInt.Mark2 == false)
                {
                    checkBox3.Enabled = false;
                }

                if (statusInt.Mark3 == false)
                {
                    checkBox4.Enabled = false;
                }

            }

        }
    }
}

我创建了一个 class 来存储我的列值

class Marks

    {
        public string Name { get; set; } // property to  store Name
        public int Age { get; set; } // property to store Age 
        public bool Marks0 { get; set; } // property to store Marks0 
        public bool Marks01 { get; set; } // property to store Marks01 
        public bool Marks2 { get; set; } // property to store Marks2 
        public bool Marks3 { get; set; } // property to store Marks3

    }

编辑:

让我看看我是否理解正确:

如果任何(或全部)Mark0true,则复选框 1 已启用。如果所有都是 false 复选框被禁用。相同的规则适用于其他标记和复选框。

首先,您没有将 statusInt 添加到您的 ObservingData 集合中。接下来,删除所有这些 if (statusInt.Mark0 == false) 检查,因为它们只检查您从 csv 读取的最新行。总结一下,这是需要做的事情:

foreach (string line in System.IO.File.ReadAllLines(tbOutputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
{
    // here line stands for each line in the CSV file

    string[] InCsvLine = line.Split(',');
    //init Marks class
    Marks statusInt = new Marks();

    statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
    statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
    statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
    statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);

    //add line read from csv to colletion
    ObservingData.Add(statusInt);
}

//instead of if (statusInt.Mark0 == false), if (statusInt.Mark1 == false) etc, add this
checkBox1.Enabled = ObservingData.Any(m => m.Marks0);
checkBox2.Enabled = ObservingData.Any(m => m.Marks1);
checkBox3.Enabled = ObservingData.Any(m => m.Marks2);
checkBox4.Enabled = ObservingData.Any(m => m.Marks3);