Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I have a textfile with columns that I want to output in a DataGridView. The line that I want to read the clumn names from isn't in the beginning of the file so I have to look for a line that starts with #COLUMNNAME and read the coulm names from that line. The problem is that when I do this C# thinks that I want to count this "#COLUMNNAME" as a column. so what I want to do is something like filtering the line and ignore the "#COLUMNNAME" word and same for the last character in the text file for that line that ends with a ';'.

C#
private void button2_Click(object sender, EventArgs e)
{
    foreach (string line in File.ReadLines(filePath))
    {
        if (line.Contains("#COLUMNNAME"))
        {
            string[] s = line.Split('\t');

            for (int i = 0; i <= s.Count() - 1; i++)
            {
                DataGridViewColumn columns = new DataGridViewTextBoxColumn();
                columns.Name = "col" + i.ToString();
                columns.HeaderText = s[i].ToString();
                dataGridView1.Columns.Add(columns);
            }
        }
    }
}


#COLUMNNAME	MapArea	MobId	MinLevel	MaxLevel	AbStateCnt	MinCen	MaxCen	CenRate	TradeBoxA	RateA	TradeBoxB	RateB	TradeBoxC	RateC	DrItem1      ;
Posted
Updated 14-May-13 3:18am
v3
Comments
[no name] 14-May-13 9:21am    
When you split the string #COLUMNNAME will end up as the 0th element in s, so why don't you simply ignore the 0th element?
maxbre 14-May-13 9:29am    
I didn't think about it. Thank you so much!

1 solution

Perhaps something like:
C#
string[] s = line.Replace("#COLUMNNAME", "").Trim().Split('\t');

That should filter out the Column name keyword...
 
Share this answer
 
v3

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