Reputation: 67
My program currently reads in the values from a text file that is in the .HRM format and will parse them depending on the column the data is contained in and insert the data into the appropriate row. The code I have currently will read in the code and below that i attempted to make the code into a list which is then inserted into the datagridview but I think I have done this incorrectly due to not having the array set up correctly as I am not sure how it would split between the columns nor how I would decide which row of the datagridview the data would be inserted into. This is the code I am using to load in the file:
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
string filePath = @"F:\testdata.hrm";
if (File.Exists(filePath))
processfile(filePath);
else
System.Console.WriteLine("Error - File {0} not Found", filePath);
System.Console.ReadKey();
}
}
// static void processfile(String filePath)
// {
// List<string[]> result = File.ReadAllLines("filePath") //read all lines in the file
// .SkipWhile(x => x != "[HRData]") //skip lines until reaching the "[HRData]"
// .Select(x => x.Split(null, StringSplitOptions.RemoveEmptyEntries)) //split line by whitespace to get numbers in line
// .ToList(); // convert the result to a list
// result.ForEach(x => dataGridView1.Rows.Add(x));
}
}
}
This is the data I am reading in:
[HRData]
91 154 70 309 83 6451
91 154 70 309 83 6451
92 160 75 309 87 5687
94 173 80 309 87 5687
96 187 87 309 95 4662
100 190 93 309 123 4407
101 192 97 309 141 4915
103 191 98 309 145 5429
106 190 99 309 157 4662
106 187 98 309 166 4399
107 186 97 309 171 4143
107 185 97 309 170 4914
108 184 96 309 171 5426
108 183 95 309 170 5688
So what I will want to do is read each column in the file then adding each column into a seperate array for each of the columns and they will then be inserted into the datagridview in the appropriate columns. I tried to code this but I'm not sure what I did wrong so any help would be appreciated.
Upvotes: 0
Views: 2506
Reputation: 8025
You can do like this:
var lines = File.ReadAllLines(@"F:\testdata.hrm").Skip(1);
foreach (var line in lines)
dataGridView1.Rows.Add(line.Split(
new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
EDIT:
Based on you comment, you want rotate row to column, and column to row. You can do like bellow:
var lines = File.ReadAllLines(@"F:\testdata.hrm").Skip(1);
List<string[]> list = new List<string[]>();
foreach (var line in lines)
{
list.Add(line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
for (int i = 0; i < 6; i++)
{
List<string> cols = new List<string>();
for (int j = 0; j < list.Count; j++)
{
cols.Add(list[j][i]);
}
dataGridView1.Rows.Add(cols.ToArray());
}
To add header to a row:
List<string> rowHeaders = new List<string> { "A", "B", "C", "D", "E", "F", "G" };
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].HeaderCell.Value = rowHeaders[i];
}
Upvotes: 1
Reputation: 11
It looks like you are implementing Linq into C# code? Also, you want to separate each column into an array of itself, but you have only declared one array? If you want each column into its own array, you need to declare multiple arrays, or you need to declare a 3-D array. I would recommend using a foreach loop that this:
foreach(string column in filepath)
{
string[] result = File.ReadAllLines("filePath");
string dataColumn = dataGridView1.Rows.Add(result));
}
Upvotes: 0