moose333 0 Light Poster

Hello

I am currently attempting to write the contents of a .hrm file into a datagridview and place each piece of data into its required column.

This is the code I have at the moment which is currently just extracting some data from the .hrm file and placing it in some labels.

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;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string date1 = "";
        string starttime1 = "";
        string length1 = "";
        string maxhr1 = "";
        string resthr1 = "";
        string vo2max1 = "";
        string weight1 = "";
        

        private void button1_Click(object sender, EventArgs e)
        {
            

            string Chosen_File = "";

            openFile.Title = "Choose File";
            openFile.FileName = "";
            openFile.Filter = "HRM|*.hrm|Text Document|*.txt";
            openFile.ShowDialog();

            if (openFile.ShowDialog() != DialogResult.Cancel)
            {
                Chosen_File = openFile.FileName;
                richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
            }


            String[] lines = File.ReadAllLines(Chosen_File); 

            IEnumerable<String> date = from n in lines            
                                       where n.StartsWith("Date") 
                                       select n.Split('=')[1];    

            IEnumerable<String> starttime = from n in lines            
                                       where n.StartsWith("StartTime") 
                                       select n.Split('=')[1];

            IEnumerable<String> length = from n in lines
                                            where n.StartsWith("Length")
                                            select n.Split('=')[1];

            IEnumerable<String> maxhr = from n in lines
                                         where n.StartsWith("MaxHR")
                                         select n.Split('=')[1];

            IEnumerable<String> resthr = from n in lines
                                        where n.StartsWith("RestHR")
                                        select n.Split('=')[1];

            IEnumerable<String> vo2max = from n in lines
                                         where n.StartsWith("VO2max")
                                         select n.Split('=')[1];

            IEnumerable<String> weight = from n in lines
                                         where n.StartsWith("Weight")
                                         select n.Split('=')[1]; 

            foreach (String d in date)
            {
                Console.WriteLine(d);
                date1 = d;
            }

            foreach (String d in starttime)
            {
                Console.WriteLine(d);
                starttime1 = d;
            }

            foreach (String d in length)
            {
                Console.WriteLine(d);
                length1 = d;
            }

            foreach (String d in maxhr)
            {
                Console.WriteLine(d);
                maxhr1 = d;
            }

            foreach (String d in resthr)
            {
                Console.WriteLine(d);
                resthr1 = d;
            }

            foreach (String d in vo2max)
            {
                Console.WriteLine(d);
                vo2max1 = d;
            }

            foreach (String d in weight)
            {
                Console.WriteLine(d);
                weight1 = d;
            }

            datelabel.Text = date1.ToString();
            startlabel.Text = starttime1.ToString();
            lengthlabel.Text = length1.ToString();
            maxhrlabel.Text = maxhr1.ToString();
            resthrlabel.Text = resthr1.ToString();
            vo2maxlabel.Text = vo2max1.ToString();
            weightlabel.Text = weight1.ToString();
        }

       


    }
}

And this is the contents of the .hrm file I am reading from with the data I want to now display under the title [HRData].

[Params]
Version=106
Monitor=34
SMode=111111100
Date=20100926
StartTime=09:20:11.0
Length=01:06:11.6
Interval=1
Upper1=0
Lower1=0
Upper2=0
Lower2=0
Upper3=180
Lower3=177
Timer1=00:00:00.0
Timer2=00:00:00.0
Timer3=00:00:00.0
ActiveLimit=0
MaxHR=195
RestHR=48
StartDelay=0
VO2max=54
Weight=70

[Note]

[IntTimes]
00:01:00.0	107	106	119	129
0	0	0	0	0	-125
0	0	0	0	0
33554432	5	0	0	0	0
0	0	0	0	0	0
00:27:05.2	171	107	173	178
0	0	0	512	0	-110
0	0	0	0	0
33554432	12487	0	0	0	0
500	0	0	0	0	0
00:57:42.9	182	170	173	181
0	0	0	486	105	-128
0	0	0	0	0
0	12304	665	110	0	0
0	0	0	0	0	0
00:58:26.3	166	166	177	182
0	0	0	186	0	-123
0	0	0	0	0
33554432	196	0	0	0	0
500	0	0	0	0	0
01:06:11.6	118	120	135	166
0	0	0	58	105	-119
0	0	0	0	0
0	1824	0	120	0	0
0	0	0	0	0	0

[IntNotes]

[ExtraData]

[Summary-123]

[Summary-TH]
3971	0	23	198	3750	0
195	180	177	48
0	3971

[HRZones]
195
175
156
136
117
97
0
0
0
0
0

[SwapTimes]

[Trip]
381
177
3971
-94
-67
397
622
4351

[HRData]
124	0	0	-124	0	50
124	0	0	-124	0	50
124	0	0	-124	0	50
123	0	0	-124	0	50
123	0	0	-124	0	50
122	0	0	-124	0	50
122	0	0	-124	0	50
122	0	0	-124	0	50
123	44	0	-124	0	50
123	53	0	-124	0	50
125	53	0	-124	0	50
125	53	0	-124	0	50
126	53	0	-124	0	50
127	53	0	-124	0	50

I have been struggling with this for a while no and if anyone can help me understand how I would go about doing this or can point me to a simpler method of displaying the data in a table I would be most grateful.

Thank you.

Dani AI

Generated

— the simplest, reliable approach is to locate the [HRData] section, read each subsequent non-empty line until the next section header, split each data line on tabs/whitespace, and push those values into a DataTable (then bind it to the DataGridView). Also note a bug in your current code: OpenFileDialog.ShowDialog() is being called twice — call it once and check the returned DialogResult.

A compact workflow:

  • Show the open dialog once and get the chosen path.
  • Stream the file line-by-line until you find [HRData].
  • For each following line: trim, stop if it starts with [ (next section) or is empty, split on \t/space using StringSplitOptions.RemoveEmptyEntries.
  • Create DataTable columns dynamically from the first data row, parse with TryParse, add rows.
  • Set dataGridView.DataSource = dataTable for fast display.

Example parsing pattern (C#) — reads the HRData block, builds a DataTable, binds it:

var result = openFile.ShowDialog();
if (result != DialogResult.OK) return;
string path = openFile.FileName;

var dt = new DataTable();
using (var sr = new StreamReader(path))
{
    string line;
    while ((line = sr.ReadLine()) != null && !line.Trim().Equals("[HRData]", StringComparison.OrdinalIgnoreCase)) { }

    while ((line = sr.ReadLine()) != null)
    {
        line = line.Trim();
        if (line.Length == 0 || line.StartsWith("[")) break;
        var parts = line.Split(new[] {'\t',' '}, StringSplitOptions.RemoveEmptyEntries);
        if (dt.Columns.Count == 0)
            for (int i = 0; i < parts.Length; i++) dt.Columns.Add("C" + (i+1), typeof(int));
        var dr = dt.NewRow();
        for (int i = 0; i < parts.Length; i++) int.TryParse(parts[i], out int v); dr[i] = v;
        dt.Rows.Add(dr);
    }
}
dataGridView1.DataSource = dt;

Extra tips: use int.TryParse/double.TryParse and CultureInfo.InvariantCulture for decimals, parse StartTime with DateTime.TryParseExact (support both HH:mm:ss and HH:mm:ss.fff), and compute per-row timestamps via start.AddSeconds(rowIndex * interval). For large files prefer streaming (not ReadAllLines). If column meanings are required, consult the HRM/Polar spec and map headers before binding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.