在DataGridView中显示文本文件

时间:2015-11-21 05:04:59

标签: c# winforms datagridview

bb--------->0.918295818426175
mn--------->1.58496250496106
bs--------->1.58496250496106
ddq--------->1.5
gg--------->0.918295818426175
ss--------->0.918295818426175
dd--------->0.918295818426175
vv--------->0.918295818426175
bbw--------->1.5
nn--------->0.918295818426175
sa dfg--------->2.52164068457594
mrg--------->2
ytt--------->1.5
po--------->1.58496250496106

这是我想要在DataGridView中显示此内容的文本文件内容 格式如下

**bb                   mn                  bs
0.918295818426175   .58496250496106      1.58496250496106**

其中字符串在列标题中,值在行中

我该怎么做?

private void button2_Click(object sender, EventArgs e)
{
    string path = @"D:\finatt.txt";
     string[] textdata = File.ReadAllLines(path);
    string[] headers = textdata[0].Split(' ');
    DataTable dt1 = new DataTable();

    foreach (string header in headers)
    {
        dt1.Columns.Add(header,typeof(string),null);

    }
    for (int i = 0; i < textdata.Length; i++)
        dt1.Rows.Add(textdata[i].Split(','));
    dataGridView1.DataSource = dt1;
}

1 个答案:

答案 0 :(得分:0)

您需要逐个处理每一行,拆分列和值,但是在创建所有列之后,您需要将行值保持到最后。

在下面的代码中,我使用StreamReader并调用ReadLine,直到返回null,表示流的结束。

每行的处理是通过将列名添加到DataTable来完成的,同时将行值存储到以后。我使用简单的通用List

每一行都是Split一个唯一的字符,在本例中为>。列名是使用Substring获得的。行值为TryParsed为double。我使用专门的重载来满足数字格式。

处理完所有行后,剩下的就是创建并添加NewRow。通过将从我们的值获得的数组分配给ItemArray来填充该行。

以上描述的实现将如下所示:

var values = new List<double?>(); // nullable, to handle parse errors

string path = @"D:\finatt.txt";
using (var sr = new StreamReader(path))
{

    string line;

    var lineCount = 0;
    while ((line = sr.ReadLine()) != null)
    {
        lineCount++;
        // line does now have this value
        // bb--------->0.918295818426175
        var split = line.Split('>');
        // in split[0] we now have bb------
        // in split[1] we now have 0.918295818426175
        // find where the - starts
        var startOfDash = split[0].IndexOf('-');
        // so we can get the column name 
        var col = split[0].Substring(0, startOfDash);
        // add that column,
        dt1.Columns.Add(col, typeof(double));
        // store split[1] in the values list to be processed later
        // parse to a double
        double value;
        if (Double.TryParse(split[1],
            NumberStyles.AllowDecimalPoint,
            new NumberFormatInfo { NumberDecimalSeparator = "." },
            out value))
        {
            values.Add(value);
        }
        else
        {
            // make sure the values array has the same
            // number of entries as we have columns
            values.Add(null);
            Trace.WriteLine(String.Format("Error on line {0}, for {1} parsing value {2}", lineCount, split[0], split[1]));
        }
    }
}
var row = dt1.NewRow();
dt1.Rows.Add(row);
// assign the values array to the row
// Cast the double the an object in the process.
row.ItemArray = values.Cast<object>().ToArray();

// done all, shows data
dataGridView1.DataSource = dt1;