The code below works for me, however I would like to add a condition before adding it to the table. What I need is if the "Scan time" is between two dates, then it should be added to the "table", if not, then it should be ignored.
This is for file selection.
private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog() { Title = "Select the file to open.", Filter = "DAT (*.dat)|*.dat|TXT (*.txt)|*.txt|All Files (*.*)|*.*", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) }; if (ofd.ShowDialog() == DialogResult.OK) { txtFilePath.Text = ofd.FileName; loadDataToGridview(); } }
This is for reading a file and then adding it to a datagridview
private void loadDataToGridview() { DataTable table = new DataTable(); table.Columns.Add("Emp ID"); table.Columns.Add("Scan Time"); table.Columns.Add("Undefined 1"); table.Columns.Add("Undefined 2"); table.Columns.Add("Undefined 3"); table.Columns.Add("Undefined 4"); var lines = File.ReadAllLines(txtFilePath.Text).ToList(); lines.ForEach(line => table.Rows.Add(line.Split((char)9))); return table; }
I got the loadDataToGridview method, but I don't know how to blow
lines.ForEach(line => table.Rows.Add(line.Split((char)9)));
lambda expression include the condition I need. Suppose the names of datepickers are dateFrom and dateTo. Your help is greatly appreciated.
source share