1

I am trying to pull that last 5 lines from a .csv file and display them in a datagrid on my form. How would I insert the data onto the datagrid?

Here is my current Code,

        int x = 5;   
        var buffor = new Queue<string>(x);

        var log = new StreamReader(@"MyPath");

        while (!log.EndOfStream)
        {
            string line = log.ReadLine();

            if (buffor.Count >= x)
                buffor.Dequeue();
            buffor.Enqueue(line);
        }

        string[] lastLines = buffor.ToArray();

Thanks in advance.

Shadow89
  • 57
  • 1
  • 6

1 Answers1

1

You can do this,

 public Form1()
        {
            InitializeComponent();
            int x = 5;
            var buffor = new Queue<string>(x);
            foreach (var headerLine in File.ReadLines("C:/NewMap.csv").Take(1))
            {
                foreach (var headerItem in headerLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    dataGridView1.Columns.Add(headerItem.ToString().Trim(), headerItem.ToString());
                }
            }
            var log = new StreamReader("C:/NewMap.csv");

            while (!log.EndOfStream)
            {
                string line = log.ReadLine();

                if (buffor.Count >= x)
                    buffor.Dequeue();
                buffor.Enqueue(line);
            }

            foreach (var line in buffor)
            {
                if (line != string.Empty || line != string.Empty)
                {
                    dataGridView1.Rows.Add(line);
                }
            }
        }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396