Split Comma separated string into multiple Column in Windows Application using C# and VB.Net

Last Reply one month ago By priyajsr

Posted one month ago

I have following data

where data before - is working hours,data after - is ot hours



I want data in following format

Working Hours            Ot Hours

0                                 16:02:04

08:00:00                     00:10:35

08:00:00                     00:10:19

08:00:00                     00:25:58

08:00:00                     01:29:01


How i will achieve it

Posted one month ago

Hey priyajsr,

Please refer below sample.



private void Form1_Load(object sender, EventArgs e)
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("Working Hours", typeof(string)), new DataColumn("Ot Hours", typeof(string)) });
    string str = "0-16:02:04,08:00:00-00:10:35,08:00:00-00:10:19,08:00:00-00:25:58,08:00:00-01:29:01";
    string[] strsplit = str.Split(',');
    foreach (string item in strsplit)
        dt.Rows.Add(item.Split('-')[0] == "0" ? "00:00:00" : item.Split('-')[0], item.Split('-')[1]);
    this.dataGridView1.DataSource = dt;


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Working Hours", GetType(String)), New DataColumn("Ot Hours", GetType(String))})
    Dim str As String = "0-16:02:04,08:00:00-00:10:35,08:00:00-00:10:19,08:00:00-00:25:58,08:00:00-01:29:01"
    Dim strsplit As String() = str.Split(","c)

    For Each item As String In strsplit
        dt.Rows.Add(If(item.Split("-"c)(0) = "0", "00:00:00", item.Split("-"c)(0)), item.Split("-"c)(1))

    Me.dataGridView1.DataSource = dt
End Sub