문제

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();
}
도움이 되었습니까?

해결책

replace

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

with

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

You are reading the first line then discarding it, before entering your loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top