Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I have text file like this... From
[ISTK-1]
to
[ISTK-1]
it should look like one row after grouping.
[ISTK-1]
can be more than
[ISTK-100]
.

[ISTK-1]
MAT=EG_W980_ST2_18
L=596
B=200
A=3
REF=EF9440
BEM=ATV-2 fasāde
BEM2=;;;*
EDGE1=2mm abs
EDGE2=2mm abs
EDGE3=2mm abs
EDGE4=2mm abs
[ISTK-1]

[ISTK-2]
MAT=EG_W980_ST2_18
L=2398
B=502
A=2
REF=EF9440
BEM=Starpsiena
BEM2=;;;*
EDGE1=2mm abs
EDGE3=2mm abs
EDGE4=2mm abs
[ISTK-2]


What I have tried:

I have tried to make windows form with C#. I have open file function working and something imports in grid view but nothing what i like. I have
FlexibleMessageBox.Show
that shows all info in file.
<a href="https://ibb.co/LnYQYxv"><img src="https://i.ibb.co/yWdhd6V/Capture.png" alt="Capture" border="0"></a>
Posted
Updated 16-Jun-21 2:20am
v2
Comments
Richard Deeming 16-Jun-21 4:31am    
There's a secret error somewhere in your secret code. You should fix that.

Seriously, how do you expect anyone to help you fix your code when we can't see your code?

Or were you expecting someone to write all of the code for you?
Maciej Los 16-Jun-21 4:34am    
:laugh:

1 solution

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):
C#
public class MyData
{
	public MyData()
	{
	 	//empty constructor;
	}
	
	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:

C#
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.
 
Share this answer
 
Comments
Endijs 16-Jun-21 8:06am    
JUST WOW! Thanks! I will try reverse engineer this to understand everything. Now everything works!
Maciej Los 16-Jun-21 10:39am    
😀 Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900