Reputation: 37
I have a .csv file like below;
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
So, I want to read this .csv file and check whether the status of Marks0, Marks1, Marks2, Marks3. After that, I want to enable or disable checkbox during the button1 click to browse this file.my interface is like this. Interface of my code
In here want to check status of Marks0, Marks1, Marks2, Marks3 the condition is like this, that selected column whole data are TRUE then enable the checkBox.but there are True and False are there then checkBox is enabled.but all are False then checkBox is Disable.
An example in here
Marks0 all are TRUE then checkBox1 is enable
Marks1 all are TRUE and FALSE then checkBox2 is enable
Marks2 all are FALSE then checkBox3 is disable
Marks3 all are TRUE and FALSE then checkBox4 is enable like that, I want to build my code.
I can enable the checkBox whole column is TRUE and I can disable the checkBox whole column is FALSE but I can not enable the checkBox that it is TRUE and False like Marks1 and Marks3.please give me a solution for this.
My code is following;
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;
}
}
}
}
}
I created a class to store my column value
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
}
Upvotes: 1
Views: 2046
Reputation: 7095
Edit:
Let me see if I understood you correctly:
if any (or all) Mark0
is true
then the checkbox1 is enabled. If ALL are false
checkbox is disabled. Same rule apply to other marks and checkboxes.
First, you're missing adding statusInt to your ObservingData
collection. Next, remove all those if (statusInt.Mark0 == false)
checks because they are checking onyl the lastest row that you have read from csv. To sum things up, this is what needs to be done:
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);
Upvotes: 0