locked
Extract out only certain rows of text from a text file RRS feed

  • Question

  • say with the given text file, as here..

       Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)
                                                                  (Test, System time)   (Test, Wait, System time)
    ==========  ==========   ========    ========    ==========   ===================   =========================
    01/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    02/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    03/12/2013  0013-002     0h 39min     0h 0min       0h 0min               7.78                       7.85
    04/12/2013  0013-002     0h 38min     0h 0min       0h 0min               7.66                       7.74
    05/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    07/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/12/2013  0013-002     5h 23min    0h 12min       0h 7min              64.80                      67.17
    10/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    11/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    12/12/2013  0013-002     0h 53min     0h 1min       0h 1min              10.66                      11.01
    13/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    14/12/2013  0013-002     1h 13min    0h 11min       0h 1min              14.64                      16.95
    15/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    16/12/2013  0013-002      0h 5min     0h 0min       0h 0min               1.13                       1.18
    17/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    18/12/2013  0013-002      2h 0min     0h 0min       0h 0min              23.74                      23.85
    19/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    20/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    21/12/2013  0013-002     4h 56min    1h 38min       0h 0min              58.24                      77.52
    22/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    23/12/2013  0013-002     0h 24min     0h 0min       0h 0min               4.84                       4.99
    24/12/2013  0013-002     4h 17min     0h 0min       0h 0min              50.55                      50.64
    25/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    26/12/2013  0013-002      0h 0min    0h 57min       0h 0min               0.05                      11.36
    27/12/2013  0013-002     5h 17min    0h 44min       0h 0min              62.34                      71.06
    28/12/2013  0013-002     3h 33min     1h 8min       0h 1min              42.06                      55.52
    29/12/2013  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    30/12/2013  0013-002     1h 18min     0h 4min       0h 1min              15.69                      16.66
    31/12/2013  0013-002      4h 5min    0h 40min       0h 2min              48.62                      56.58
    01/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    02/01/2014  0013-002     1h 29min    0h 42min       0h 0min              17.74                      26.04
    03/01/2014  0013-002      6h 0min     0h 1min       0h 1min              70.94                      71.17
    04/01/2014  0013-002     4h 40min    0h 56min       0h 1min              55.30                      66.35
    05/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    07/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    10/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    11/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    12/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    13/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    14/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    15/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    16/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    17/01/2014  0013-002      0h 0min     0h 0min       0h 0min               0.00                       0.00
    Total       0013-002    46h 58min    7h 22min      0h 20min              11.60                      13.41
    01/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    02/12/2013  0013-032     1h 32min    0h 25min       0h 1min              18.30                      23.31
    03/12/2013  0013-032      4h 3min    0h 15min       0h 3min              48.38                      51.33
    04/12/2013  0013-032     2h 45min    0h 53min       0h 1min              32.78                      43.18
    05/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/12/2013  0013-032     3h 46min     0h 6min       0h 4min              45.22                      46.56
    07/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/12/2013  0013-032     2h 32min     0h 9min       0h 1min              30.16                      32.10
    10/12/2013  0013-032     3h 27min    1h 11min       0h 2min              41.08                      55.15
    11/12/2013  0013-032     0h 17min     0h 0min       0h 0min               3.34                       3.37
    12/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    13/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    14/12/2013  0013-032     1h 43min    0h 47min       0h 2min              20.76                      30.05
    15/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.10                       0.10
    16/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    17/12/2013  0013-032     2h 37min    0h 10min       0h 0min              30.95                      32.97
    18/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    19/12/2013  0013-032      2h 5min     0h 6min       0h 1min              24.75                      26.02
    20/12/2013  0013-032      0h 5min     0h 2min       0h 1min               1.34                       1.85
    21/12/2013  0013-032     2h 39min    0h 32min       0h 1min              31.51                      37.81
    22/12/2013  0013-032      0h 2min     0h 0min       0h 0min               0.53                       0.60
    23/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    24/12/2013  0013-032      5h 5min     0h 8min       0h 1min              60.09                      61.69
    25/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    26/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    27/12/2013  0013-032     0h 47min     0h 0min       0h 0min               9.42                       9.43
    28/12/2013  0013-032      2h 6min    0h 32min       0h 1min              25.04                      31.44
    29/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    30/12/2013  0013-032     1h 40min     0h 0min       0h 0min              19.76                      19.78
    31/12/2013  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    01/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    02/01/2014  0013-032     0h 25min     0h 0min       0h 0min               5.11                       5.16
    03/01/2014  0013-032     6h 31min     0h 1min       0h 0min              76.82                      77.05
    04/01/2014  0013-032     0h 12min     0h 3min       0h 1min               2.77                       3.50
    05/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    07/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    10/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    11/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    12/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    13/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    14/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    15/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    16/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    17/01/2014  0013-032      0h 0min     0h 0min       0h 0min               0.00                       0.00
    Total       0013-032    44h 26min    5h 27min      0h 27min              11.00                      12.34
    01/12/2013  0013-060      0h 0min     0h 0min       0h 2min               0.42                       0.42
    02/12/2013  0013-060     5h 43min     0h 2min       0h 1min              67.78                      68.29
    03/12/2013  0013-060     5h 16min     0h 5min       0h 3min              62.71                      63.71
    04/12/2013  0013-060     5h 16min     0h 0min       0h 0min              62.15                      62.19
    05/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    07/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/12/2013  0013-060      0h 0min     0h 2min       0h 0min               0.06                       0.58
    10/12/2013  0013-060     5h 12min     0h 0min       0h 0min              61.34                      61.34
    11/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    12/12/2013  0013-060     6h 15min     0h 0min       0h 0min              73.70                      73.71
    13/12/2013  0013-060     2h 33min     0h 8min       0h 0min              30.22                      31.96
    14/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    15/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    16/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    17/12/2013  0013-060     1h 28min    0h 30min       0h 0min              17.32                      23.35
    18/12/2013  0013-060     2h 23min     0h 0min       0h 0min              28.30                      28.49
    19/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    20/12/2013  0013-060     5h 28min     0h 2min       0h 0min              64.56                      65.12
    21/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.03                       0.14
    22/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    23/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    24/12/2013  0013-060     1h 44min     0h 1min       0h 0min              20.64                      20.96
    25/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    26/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    27/12/2013  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    28/12/2013  0013-060     5h 49min    0h 28min       0h 0min              68.55                      74.19
    29/12/2013  0013-060      0h 0min     0h 2min       0h 0min               0.17                       0.74
    30/12/2013  0013-060     5h 59min    0h 41min       0h 0min              70.58                      78.69
    31/12/2013  0013-060     5h 35min     0h 3min       0h 1min              66.25                      66.88
    01/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.15                       0.15
    02/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    03/01/2014  0013-060     0h 35min     0h 3min       0h 1min               7.08                       7.81
    04/01/2014  0013-060     5h 43min     0h 0min       0h 1min              67.49                      67.68
    05/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    06/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    07/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    08/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    09/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    10/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    11/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    12/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    13/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    14/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    15/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    16/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    17/01/2014  0013-060      0h 0min     0h 0min       0h 0min               0.00                       0.00
    Total       0013-060     65h 6min    2h 17min      0h 18min              16.03                      16.59

    when user click a Yes radio button, the above would be displayed. What I want is when user clicks a No radio button, below is shown:

       Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)
                                                                  (Test, System time)   (Test, Wait, System time)
    ==========  ==========   ========    ========    ==========   ===================   =========================
    Total       0013-002    46h 58min    7h 22min      0h 20min              11.60                      13.41
    Total       0013-032    44h 26min    5h 27min      0h 27min              11.00                      12.34
    Total       0013-060     65h 6min    2h 17min      0h 18min              16.03                      16.59

    when user clicks back the Yes radio button, those details would be shown again... so we need to save 2 text files with and without details?

    thanks.

    Monday, January 6, 2014 4:09 AM

Answers

  • Yes

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Globalization;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
           //Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)
           //
           //
           //                                                      (Test, System time)   (Test, Wait, System time)
           DataTable  table;
          
           const string FILENAME = @"c:\temp\test.txt";
        
           public struct ColumnDef
           {
              public int colStart;
              public int colLength;
           }
        
           public ColumnDef[] columns = { 
              new ColumnDef  {colStart = 1, colLength = 10},
              new ColumnDef  {colStart = 12, colLength = 10},
              new ColumnDef  {colStart = 25, colLength = 8},
              new ColumnDef  {colStart = 37, colLength = 8},
              new ColumnDef  {colStart = 49, colLength = 10},
              new ColumnDef  {colStart = 62, colLength = 20},
              new ColumnDef  {colStart = 85, colLength = 25},
           };
    
            public Form1()
            {
                InitializeComponent();
                table = new DataTable();
    
                table.Columns.Add("Date", typeof(Object));
                table.Columns.Add("SystemType", typeof(String));
                table.Columns.Add("Testtime", typeof(TimeSpan));
                table.Columns.Add("Waittime", typeof(TimeSpan));
                table.Columns.Add("SystemTime", typeof(TimeSpan));
                table.Columns.Add("Utilization", typeof(Double));
                table.Columns.Add("Utilization-Wait", typeof(Double));
            
                StreamReader reader = new StreamReader(FILENAME);
                Boolean foundHeaderLine = false;
                object[] inputArrayObject = new object[7];
                int rowcount  = 0;
    
                while(!reader.EndOfStream)
                {
                    string inputLine  = reader.ReadLine();
                    rowcount += 1;
                    if (foundHeaderLine == false)
                    {
                        if (inputLine.StartsWith("=========="))
                        {
                            foundHeaderLine = true;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < columns.Length; i++)
                        {
                            switch (i)
                            {
                                case 0:
                                    if (inputLine.StartsWith("Total"))
                                    {
                                        inputArrayObject[i] = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    }
                                    else
                                    {
                                        inputArrayObject[i] = DateTime.ParseExact(inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim(),
                                            "dd/MM/yyyy", CultureInfo.InvariantCulture);
                                    }
                                    break;
    
                               case 1:
                                    inputArrayObject[i] = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    break;
    
                               case 2:
                               case 3:
                               case 4:
                                    string timeStr = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    timeStr = timeStr.Replace("h ", ":");
                                    timeStr = timeStr.Replace("mi", "");
    
                                    string[] splitTime = timeStr.Split(new char[] { ':' });
                                    inputArrayObject[i] = new TimeSpan(int.Parse(splitTime[0]), int.Parse(splitTime[1]), 0);
                                    break;
    
                               case 5:
                                    inputArrayObject[i] = Double.Parse(inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim());
                                    break;
                               case 6:
                                    inputArrayObject[i] = Double.Parse(inputLine.Substring(columns[i].colStart - 1).Trim());
                                    break;
                            } //end switch
    
                        } // end for
                        try
                        {
                             DataRow newRow = table.Rows.Add();
                             newRow.ItemArray = inputArrayObject;
                        }
                        catch (Exception ex)
                        {
                        }
                    } // end else
                    
                } // end while
                        
                dataGridView1.DataSource = table;
                reader.Close();
                reader.Dispose();
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox chkbx   = sender as  CheckBox;
                DataRow[]  displayRows = null;
                if(chkbx.Checked == true)
                {
                   displayRows = table.AsEnumerable()
                      .Where(x => x.Field<Object>("Date").ToString() == "Total")
                      .ToArray();
                }
                else
                {
                displayRows = table.AsEnumerable()
                   .Where(x => x.Field<Object>("Date").ToString() != "Total")
                   .ToArray();
                }
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = displayRows.CopyToDataTable();
                
    
            }
        }
    }
    
    


    jdweng

    • Proposed as answer by Ramya SJ Friday, January 10, 2014 8:15 AM
    • Marked as answer by Eason_H Monday, January 13, 2014 1:47 AM
    Tuesday, January 7, 2014 4:46 AM

All replies

  • The best way of handling this situation is to read all the data into a datatable.  Then use Linq to filter the datatable either Date == Total or Date != Total. Load the datatable so the column is saved as text.  I can write the code a little later if nobody else response.  It only takes a few minutes. 


    jdweng

    Monday, January 6, 2014 7:59 AM
  • I took me much longer than expected to input your text files.  the task was very simple, your data wasn't in a format that made the job simple.  The time span was tricky to work with expecially whan the hours was greater than 24.

    Imports System.Data
    Imports System.IO
    Imports System.Globalization
    Public Class Form1
        'Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)
        '
        '                                                      (Test, System time)   (Test, Wait, System time)
        Dim table As DataTable
        Const FILENAME As String = "c:\temp\test.txt"
        Const CUSTOMDATETIME As String = "H. m"
        Public Structure ColumnDef
            Public colStart As Integer
            Public colLength As Integer
        End Structure
        Public columns As ColumnDef() = { _
           New ColumnDef With {.colStart = 1, .colLength = 10}, _
           New ColumnDef With {.colStart = 12, .colLength = 10}, _
           New ColumnDef With {.colStart = 25, .colLength = 8}, _
           New ColumnDef With {.colStart = 37, .colLength = 8}, _
           New ColumnDef With {.colStart = 49, .colLength = 10}, _
           New ColumnDef With {.colStart = 62, .colLength = 20}, _
           New ColumnDef With {.colStart = 85, .colLength = 25} _
           }
        Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            table = New DataTable
            table.Columns.Add("Date", GetType(Object))
            table.Columns.Add("SystemType", GetType(String))
            table.Columns.Add("Testtime", GetType(TimeSpan))
            table.Columns.Add("Waittime", GetType(TimeSpan))
            table.Columns.Add("SystemTime", GetType(TimeSpan))
            table.Columns.Add("Utilization", GetType(Double))
            table.Columns.Add("Utilization-Wait", GetType(Double))
            Dim reader As New StreamReader(FILENAME)
            Dim foundHeaderLine As Boolean = False
            Dim inputArrayObject(0 To 6) As Object
            Dim rowcount As Integer = 0
            While Not reader.EndOfStream
                Dim inputLine As String = reader.ReadLine
                rowcount += 1
                If foundHeaderLine = False Then
                    If inputLine.StartsWith("==========") Then
                        foundHeaderLine = True
                    End If
                Else
                    For i = 0 To (columns.Count - 1)
                        Select Case i
                            Case 0
                                If inputLine.StartsWith("Total") Then
                                    inputArrayObject(i) = inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim
                                Else
                                    Dim x = inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim
                                    inputArrayObject(i) = DateTime.ParseExact(inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim, _
                                        "dd/MM/yyyy", CultureInfo.InvariantCulture)
                                End If
                            Case 1
                                inputArrayObject(i) = inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim
                            Case 2, 3, 4
                                Dim timeStr As String = inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim
                                timeStr = timeStr.Replace("h ", ":")
                                timeStr = timeStr.Replace("mi", "")
                                Dim splitTime() As String = timeStr.Split(":".ToCharArray)
                                inputArrayObject(i) = New TimeSpan(Integer.Parse(splitTime(0)), Integer.Parse(splitTime(1)), 0)
                            Case 5
                                inputArrayObject(i) = Double.Parse(inputLine.Substring(columns(i).colStart - 1, columns(i).colLength).Trim)
                            Case 6
                                Dim x = inputLine.Substring(columns(i).colStart - 1).Trim
                                inputArrayObject(i) = Double.Parse(inputLine.Substring(columns(i).colStart - 1).Trim)
                        End Select
                    Next i
                    Try
                        Dim newRow As DataRow = table.Rows.Add
                        newRow.ItemArray = inputArrayObject
                    Catch
                    End Try
                End If
            End While
            DataGridView1.DataSource = table
            reader.Close()
            reader.Dispose()
        End Sub
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            Dim chkbx As CheckBox = CType(sender, CheckBox)
            Dim displayRows() As DataRow
            If chkbx.Checked = True Then
                displayRows = table.AsEnumerable() _
                   .Where(Function(x) x.Field(Of Object)("Date").ToString() = "Total") _
                   .ToArray()
            Else
                displayRows = table.AsEnumerable() _
                   .Where(Function(x) x.Field(Of Object)("Date").ToString() <> "Total") _
                   .ToArray()
            End If
            DataGridView1.DataSource = Nothing
            DataGridView1.DataSource = displayRows.CopyToDataTable
        End Sub
    End Class


    jdweng

    Monday, January 6, 2014 12:50 PM
  • do you have the code in C#, instead of VB?
    Tuesday, January 7, 2014 2:02 AM
  • Yes

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Globalization;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
           //Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)
           //
           //
           //                                                      (Test, System time)   (Test, Wait, System time)
           DataTable  table;
          
           const string FILENAME = @"c:\temp\test.txt";
        
           public struct ColumnDef
           {
              public int colStart;
              public int colLength;
           }
        
           public ColumnDef[] columns = { 
              new ColumnDef  {colStart = 1, colLength = 10},
              new ColumnDef  {colStart = 12, colLength = 10},
              new ColumnDef  {colStart = 25, colLength = 8},
              new ColumnDef  {colStart = 37, colLength = 8},
              new ColumnDef  {colStart = 49, colLength = 10},
              new ColumnDef  {colStart = 62, colLength = 20},
              new ColumnDef  {colStart = 85, colLength = 25},
           };
    
            public Form1()
            {
                InitializeComponent();
                table = new DataTable();
    
                table.Columns.Add("Date", typeof(Object));
                table.Columns.Add("SystemType", typeof(String));
                table.Columns.Add("Testtime", typeof(TimeSpan));
                table.Columns.Add("Waittime", typeof(TimeSpan));
                table.Columns.Add("SystemTime", typeof(TimeSpan));
                table.Columns.Add("Utilization", typeof(Double));
                table.Columns.Add("Utilization-Wait", typeof(Double));
            
                StreamReader reader = new StreamReader(FILENAME);
                Boolean foundHeaderLine = false;
                object[] inputArrayObject = new object[7];
                int rowcount  = 0;
    
                while(!reader.EndOfStream)
                {
                    string inputLine  = reader.ReadLine();
                    rowcount += 1;
                    if (foundHeaderLine == false)
                    {
                        if (inputLine.StartsWith("=========="))
                        {
                            foundHeaderLine = true;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < columns.Length; i++)
                        {
                            switch (i)
                            {
                                case 0:
                                    if (inputLine.StartsWith("Total"))
                                    {
                                        inputArrayObject[i] = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    }
                                    else
                                    {
                                        inputArrayObject[i] = DateTime.ParseExact(inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim(),
                                            "dd/MM/yyyy", CultureInfo.InvariantCulture);
                                    }
                                    break;
    
                               case 1:
                                    inputArrayObject[i] = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    break;
    
                               case 2:
                               case 3:
                               case 4:
                                    string timeStr = inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim();
                                    timeStr = timeStr.Replace("h ", ":");
                                    timeStr = timeStr.Replace("mi", "");
    
                                    string[] splitTime = timeStr.Split(new char[] { ':' });
                                    inputArrayObject[i] = new TimeSpan(int.Parse(splitTime[0]), int.Parse(splitTime[1]), 0);
                                    break;
    
                               case 5:
                                    inputArrayObject[i] = Double.Parse(inputLine.Substring(columns[i].colStart - 1, columns[i].colLength).Trim());
                                    break;
                               case 6:
                                    inputArrayObject[i] = Double.Parse(inputLine.Substring(columns[i].colStart - 1).Trim());
                                    break;
                            } //end switch
    
                        } // end for
                        try
                        {
                             DataRow newRow = table.Rows.Add();
                             newRow.ItemArray = inputArrayObject;
                        }
                        catch (Exception ex)
                        {
                        }
                    } // end else
                    
                } // end while
                        
                dataGridView1.DataSource = table;
                reader.Close();
                reader.Dispose();
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox chkbx   = sender as  CheckBox;
                DataRow[]  displayRows = null;
                if(chkbx.Checked == true)
                {
                   displayRows = table.AsEnumerable()
                      .Where(x => x.Field<Object>("Date").ToString() == "Total")
                      .ToArray();
                }
                else
                {
                displayRows = table.AsEnumerable()
                   .Where(x => x.Field<Object>("Date").ToString() != "Total")
                   .ToArray();
                }
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = displayRows.CopyToDataTable();
                
    
            }
        }
    }
    
    


    jdweng

    • Proposed as answer by Ramya SJ Friday, January 10, 2014 8:15 AM
    • Marked as answer by Eason_H Monday, January 13, 2014 1:47 AM
    Tuesday, January 7, 2014 4:46 AM
  • thank you very much.
    Friday, January 10, 2014 1:12 AM
  • say, based on its input file which contains all its details (of individual dates), is it possible to extract ONLY that particular row with the word 'Total'? we could possibly write for function for this? would this be a much simpler solution?
    Monday, January 13, 2014 1:57 AM
  • Most of the code is inside the for loop to extract the data from the rows.  In this code I have to split the fields which is complicated because you have a time with spaces like "5h 43min".    I had to extract the fields based on the start position in the row which added some of the complication.  Also I had to parse the time which wasn't a standard format especially where the total time row had hours greater the 24 hours.  I also had to handle different type fields DataTime, string, and TimeSpan.    


    jdweng

    Monday, January 13, 2014 7:02 AM
  • it is fine, thanks a lot... I have modified it with an another function such as below, and it works to my needs, thanks for your effort.
            private void extractTextFile()
            {
                StreamWriter sw = new StreamWriter(string.Concat(path, outputfilename_total));
                string header1 = "   Date     SystemType   Testtime    Waittime    Systemtime     Utilisation (%)          Utilisation (%)";
                string header2 = "                                                              (Test, System time)   (Test, Wait, System time)";
                string header3 = "==========  ==========   ========    ========    ==========   ===================   =========================";
                sw.WriteLine(header1);
                sw.WriteLine(header2);
                sw.WriteLine(header3);
                if (!File.Exists(string.Concat(path, outputfilename)))
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show("The file Utilisation.txt does not exist.", "File does not exist");
                    sw.Close();
                    return;
                }
                StreamReader reader = new StreamReader(string.Concat(path, outputfilename));
                while (!reader.EndOfStream)
                {
                    string inputLine = reader.ReadLine();              
                    if (inputLine.StartsWith("Total"))
                    {
                        sw.WriteLine(inputLine);
                    }
                }
                sw.Close();
                reader.Close();
                reader.Dispose();
            }

    Monday, January 13, 2014 7:28 AM