If you would like to display data in DataGridView control, then
STOP thinking about lines (strings) and
START thinking about objects!
Seems, that every line between
[ISTK-N]
tags is a record (object) with custom fields (properties), like: MAT, L, B, A, REF, BEM, BEM2, EDGE1, EDGE2, EDGE3 and EDGE4. So, you need to create custom class, which will represent that object (record):
public class MyData
{
public MyData()
{
}
public MyData(string sFileName)
{
LoadData(sFileName);
}
public string ISTK { get; set;}
public string MAT { get; set;}
public int L { get; set;}
public int B { get; set;}
public int A { get; set;}
public string REF { get; set;}
public string BEM { get; set;}
public string BEM2 { get; set;}
public string EDGE1 { get; set;}
public string EDGE2 { get; set;}
public string EDGE3 { get; set;}
public string EDGE4 { get; set;}
public List<MyData> LoadData(string sFileName)
{
List<MyData> internallist = new List<MyData>();
MyData md = null;
List<string> lines = File.ReadAllLines(sFileName).Where(x=>x.Trim().Length>0).ToList();
string pattern = @"\[ISTK\-\d{1,}\]";
Regex r = new Regex(pattern);
foreach(string line in lines)
{
if (r.IsMatch(line))
{
md = new MyData(){ISTK= line.Replace("[", "").Replace("]", "")};
if(internallist.Where(x=>x.ISTK.Equals(md.ISTK)).SingleOrDefault()==null) internallist.Add(md);
}
else
{
string[] props = line.Split('=');
switch(props[0])
{
case "MAT":
md.MAT = props[1];
break;
case "L":
md.L = Convert.ToInt32(props[1]);
break;
case "B":
md.B = Convert.ToInt32(props[1]);
break;
case "A":
md.A = Convert.ToInt32(props[1]);
break;
case "REF":
md.REF = props[1];
break;
case "BEM":
md.BEM = props[1];
break;
case "BEM2":
md.BEM2 = props[1];
break;
case "EDGE1":
md.EDGE1 = props[1];
break;
case "EDGE2":
md.EDGE2 = props[1];
break;
case "EDGE3":
md.EDGE3 = props[1];
break;
case "EDGE4":
md.EDGE4 = props[1];
break;
}
}
}
return internallist;
}
}
Then, you'll be able to use it like this:
List<MyData> mdlist = new MyData().LoadData(@"FullFileName.stk");
DataGridView1.DataSource = mdlist;
That's all!
Note: There's few other ways to achieve that, but it should be enough for start.