DataGridView从textfile设置colums [英] DataGridView setting up colums from textfile
问题描述
您好。我有一个文本文件,其中包含我想在DataGridView中输出的列。我想要读取clumn名称的行不在文件的开头,因此我必须查找以#COLUMNNAME开头的行并从该行读取coulm名称。问题在于,当我这样做时,C#认为我想把这个#COLUMNNAME算作一个列。所以我想要做的就是过滤行并忽略#COLUMNNAME这个词,对于那行以'';''结尾的文本文件中的最后一个字符也是如此。
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 '';''.
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 ;
推荐答案
也许类似于:
Perhaps something like:
string[] s = line.Replace("#COLUMNNAME", "").Trim().Split('\t');
这应该过滤掉列名关键字......
That should filter out the Column name keyword...
这篇关于DataGridView从textfile设置colums的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!