1

I have a DataGridView in C# and i am able to edit what is in it while the program is active but i am wondering how to add data to it programmatically. This way when the program starts i can already have data in their. By the way, this is all using windows forms. Thanks!

~I am getting data from a flat text file. if you don't know what this is it is basically using a text file as a database in a more sloppy way. So for each line of data coming in from the text file, i want to split up the line and put it into a DataGridView.

cwsl26
  • 33
  • 6

1 Answers1

0

If you don't want to use data binding, this pattern works well

string[] row = new string[numColumns];
List<string> lines;
// Add code here to read and parse your file
// Each file line is added to lines
  foreach (var line in lines)
  {
        // Split the line into fields, tab assumed here
        string[] fields= line.Split(new char[] { '\t' });
        // Add validation of file contents
        row[0] = fields[0];
        row[1] = fields[1];
        // etc.
        dataGridView1.Rows.Add(row);
    }
Peter Bill
  • 508
  • 3
  • 12