how to import dat files into datagridview with VB.NET

49 views Asked by At

I'm Trying to import dat files into datagridview with VB.NET.

But Result in one column in datagridview.

is there something wrong with my code? . Please Guide me.

Thanks

Dim dt As DataTable = New DataTable()  
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'openFileDialog1.ShowDialog()
        'TextBox1.Text = openFileDialog1.FileName
        'BindData(TextBox1.Text)
        dt.Columns.Add("ID", GetType(String))
        dt.Columns.Add("DATE", GetType(Date))
        dt.Columns.Add("TIME", GetType(String))
        dt.Columns.Add("FP", GetType(String))
        dt.Columns.Add("IN/OUT", GetType(String))
        dt.Columns.Add("OTHERS1", GetType(String))
        dt.Columns.Add("OTHERS2", GetType(String))

        Dim fName As String = ""
        OpenFileDialog1.InitialDirectory = "D:\TestFile"
        OpenFileDialog1.Filter = "Dat files(*.Dat)|*.Dat"
        OpenFileDialog1.RestoreDirectory = True
        If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
            fName = OpenFileDialog1.FileName
        End If

        Dim TextLine As String = ""
        Dim SplitLine() As String

        If System.IO.File.Exists(fName) = True Then
            Dim objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
            Dim index As Integer = 0
            Do While objReader.Peek() <> -1
                If index > 0 Then
                    TextLine = objReader.ReadLine()
                    'SplitLine = Split(TextLine, "|")
                    SplitLine = Split(TextLine, CType(vbTab, Char()))
                    dt.Rows.Add(SplitLine)
                Else
                    TextLine = objReader.ReadLine()
                End If
                index = index + 1
            Loop
            DataGridView1.DataSource = dt
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

result in datagridview

RESULT IN DATAGRIDVIEW after update code

RESULT IN DATAGRIDVIEW after update code

Sample data


     5010   2024-03-18 06:59:10 1   0   0   0
     5014   2024-03-18 07:11:00 1   0   0   0
     5014   2024-03-18 16:32:09 1   1   0   0
     5010   2024-03-18 16:33:19 1   1   0   0
     5010   2024-03-19 07:05:15 1   0   0   0
     5014   2024-03-19 07:31:19 1   0   0   0
     5014   2024-03-19 16:30:50 1   1   0   0
     5010   2024-03-19 16:31:12 1   1   0   0
    

Desired Result

for columns IN/OUT if the value is 0 then it means IN and if the value is 1 it means OUT

ID DATE TIME FP IN/OUT OTHERS1 OTHERS2
5010 2024-03-18 06:59:10 1 IN 0 0
5014 2024-03-18 07:11:00 1 IN 0 0
5014 2024-03-18 16:32:09 1 OUT 0 0
5010 2024-03-18 16:33:19 1 OUT 0 0
5010 2024-03-19 07:05:15 1 IN 0 0
5014 2024-03-19 07:31:19 1 IN 0 0
5014 2024-03-19 16:30:50 1 OUT 0 0
5010 2024-03-19 16:31:12 1 OUT 0 0
0

There are 0 answers