不想跳过第一行?

时间:2014-04-02 14:27:21

标签: c# asp.net datatable streamreader

protected void Button1_Click(object sender, EventArgs e)
{   
    string file = Server.MapPath("~/Data/") +"010414.txt";

    StreamReader reader = new StreamReader(file);
    string line = reader.ReadLine();
    DataTable dt = new DataTable();
    // DataRow row;

    while (reader.Peek() >= 0)
    {
        line = reader.ReadLine();
        string[] fields = line.Split(',');
     if (dt.Columns.Count ==0)
        {
            foreach (string field in fields)
            {
                // will add default names like "Column1", "Column2", and so on
                dt.Columns.Add();
            }
        }

        dt.Rows.Add(fields);

    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

1 个答案:

答案 0 :(得分:2)

替换

StreamReader reader = new StreamReader(file);
string line = reader.ReadLine();

StreamReader reader = new StreamReader(file);
string line;

在进入循环之前,您正在读取第一行然后丢弃它。