Faster way of reading csv to grid

后端 未结 4 464
野性不改
野性不改 2020-12-12 04:51

I have following in Windows Forms .NET 3.5

It works fine for csv with records less than 10,000 but is slower for records above 30,000. Input csv file can can any re

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 05:34

    List rows = File.ReadAllLines("Path").Select(x => x.Split(',')).ToList();
    DataTable dt = new DataTable();
    dt.Columns.Add("1");
    dt.Columns.Add("2");
    rows.ForEach(x => {
      dt.Rows.Add(x);
    });
    dgv.DataSource = dt;
    

    Try that, I suspected that you have some form of column names in the datagrid for now I just made them 1 and 2.

    To filter as per your original code use:

    List rows = File.ReadAllines("Path").Select(x => x.Split(',')).Where(x => x[0] != "" && x[1] != "").ToList();
    

    To get your columns from the DataGridView

      dt.Columns.AddRange(dgv.Columns.Cast().Select(x => new DataColumn(x.Name)).ToArray());
    

提交回复
热议问题