Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.

is there any way I could display a data to the datagrid view Columns through depending on the value of a cell. I was working for a program relating a time and attendance system that would disperse the data from the csv file to the correct data column on the datagrid view. What I want to display is the time in and time out of the specific employee in one row, but what was happening was the time in and time out creates 2 row (1 for the time in and 1 for the time out)

Here's the code details:

Dim csvreader As New StreamReader(txtboxcsv.Text, System.Text.Encoding.Default)
Dim sline As String = ""
sline = csvreader.ReadLine
    If sline Is Nothing Then Exit Do
    Dim columns() As String = sline.Split(",")
    Dim row As DataRow = datatable.NewRow

        row("Employee Number") = columns(0)
        If columns(2) = "IN" Then
            row("In Date") = columns(1)
        ElseIf columns(2) = "OUT" Then
            row("Out Date") = columns(1)
        End If
        row("In Location") = "PAM"
        row("Out Location") = "PAM"
        Dim d As String
        d = Convert.ToDateTime(columns(1)).ToString("dd-MMM-yyyy")
        row("Shift Starttime") = d + " " + "9:00"
        row("ShiftEndTime") = d + " " + "18:00"
        row("Attendance Date") = d
        row("Time Zone") = 150
    Catch ex As Exception
    End Try

DataGridView1.DataSource = datatable
Me.Text = datatable.Rows.Count & "rows"

What I have tried:

couldn't find the best condition i could use.
Updated 12-Aug-19 22:24pm
Maciej Los 13-Aug-19 8:53am
What's your input? Please, provide sample data (of text file).
Richard MacCutchan 15-Aug-19 2:56am
It appears that you need to read two rows of the csv data for one row of the DataGridView. You should only create a new row when you find a new employee entry.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

A better way is to use the Jet or ACE engines to read the .csv into a form that you can bind directly to the DataGridView. See Using OleDb to Import Text Files (tab, CSV, custom)[^], it's C# but should be fairly easy to convert.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100