Hi kana250688,
For start and end date I have made use of DateTimePicker control.
I have also used a Button for filtering the records based on specified range of date.
I am making use of LINQ to filter data according to date.
Form Design
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Linq;
VB.Net
Imports System.IO
Imports System.Linq
Public class
C#
public class datfiles
{
public string ID { get; set; }
public string DATE { get; set; }
public string TIME { get; set; }
public string FP { get; set; }
public string IN_OUT { get; set; }
public string OTHERS1 { get; set; }
public string OTHERS2 { get; set; }
}
VB.Net
Public Class datfiles
Public Property ID As String
Public Property _DATE As String
Public Property TIME As String
Public Property FP As String
Public Property IN_OUT As String
Public Property OTHERS1 As String
Public Property OTHERS2 As String
End Class
C#
private void Form1_Load(object sender, EventArgs e)
{
this.ReadDatFile();
}
private void ReadDatFile()
{
openFileDialog1.InitialDirectory = @"E:\TestFile";
openFileDialog1.Filter = "Dat files(*.Dat)|*.Dat";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ShowDialog();
this.Activate();
}
private void OnFilter(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(openFileDialog1.FileName);
List<datfiles> datfiles = new List<datfiles>();
for (int i = 0; i <= lines.Length - 1; i++)
{
if (!string.IsNullOrEmpty(lines[i]))
{
string[] data = lines[i].Split('\t');
datfiles.Add(new datfiles
{
ID = data[0],
DATE = data[1],
TIME = data[2],
FP = data[3],
IN_OUT = data[4],
OTHERS1 = data[5],
OTHERS2 = data[6]
});
}
}
DateTime dtStart = Convert.ToDateTime(startDate.Value).Date;
DateTime dtEnd = Convert.ToDateTime(endDate.Value).Date;
datfiles = datfiles.Where(x => Convert.ToDateTime(x.DATE) >= dtStart && Convert.ToDateTime(x.DATE) <= dtEnd).ToList();
dataGridView1.DataSource = datfiles;
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.ReadDatFile()
End Sub
Private Sub ReadDatFile()
OpenFileDialog1.InitialDirectory = "E:\TestFile"
OpenFileDialog1.Filter = "Dat files(*.Dat)|*.Dat"
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.ShowDialog()
Me.Activate()
End Sub
Private Sub OnFilter(ByVal sender As Object, ByVal e As EventArgs) Handles btnFilter.Click
Dim lines As String() = File.ReadAllLines(OpenFileDialog1.FileName)
Dim datfiles As List(Of datfiles) = New List(Of datfiles)()
For i As Integer = 0 To lines.Length - 1
If Not String.IsNullOrEmpty(lines(i)) Then
Dim data As String() = lines(i).Split(vbTab)
datfiles.Add(New datfiles With {
.ID = data(0),
._DATE = data(1),
.TIME = data(2),
.FP = data(3),
.IN_OUT = data(4),
.OTHERS1 = data(5),
.OTHERS2 = data(6)
})
End If
Next
Dim dtStart As DateTime = Convert.ToDateTime(startDate.Value).Date
Dim dtEnd As DateTime = Convert.ToDateTime(endDate.Value).Date
datfiles = datfiles.Where(Function(x) Convert.ToDateTime(x._DATE) >= dtStart AndAlso Convert.ToDateTime(x._DATE) <= dtEnd).ToList()
DataGridView1.DataSource = datfiles
End Sub
Public Class datfiles
Public Property ID As String
Public Property _DATE As String
Public Property TIME As String
Public Property FP As String
Public Property IN_OUT As String
Public Property OTHERS1 As String
Public Property OTHERS2 As String
End Class
Screenshot