0

I have a program with 2 Forms on it. Essentially it is a program that will put data from a CSV file into a DataGrid.

The first form has a ListBox on it displaying several folders from a hard coded directory. My objective is to select one of my subdirectories on the ListBox, then use a method to find a CSV file in this directory, and transfer all the data from the CSV file to the DataGrid on my program.

Form 2 is designed to display the data from the CSV file onto a DataGrid once I click a report button on Form 1. My code worked perfectly when I tested it out by using a temporary data grid on Form 1, but once I tried the same thing on Form 2 the DataGrid just comes up empty??

I tested out Form 2's DataGrid by hardcoding one of the paths right into it and it worked, I just cant seem to be able to take the string from my ListBox on Form 1 and use it on Form 2??

Here is the string getting the text from Form 1's ListBox:

public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    pPath = listBox1.GetItemText(listBox1.SelectedItem);
    label1.Text = pPath; //Sets the Label to CsvPath
}

Here is the code from Form 2 that's supposed to display the data on Form 2' DataGrid;

var reader1 = new StreamReader(File.OpenRead(GetCsvFile1(frm1.pPath)));
List<DateTime> listA_DateTime = new List<DateTime>();
List<string> listB_Location = new List<string>();
List<decimal> listC_Debt = new List<decimal>();
List<decimal> listD_Credit = new List<decimal>();
List<decimal> listE_Balance = new List<decimal>();

while (!reader1.EndOfStream)
{
     string line = reader1.ReadLine();
     string[] values = line.Split(',');

     listA_DateTime.Add(Convert.ToDateTime(values[0], CultureInfo.InvariantCulture));
     listB_Location.Add(values[1]);
     if (values[2] == "")
     {
          listC_Debt.Add(0);
     }
     else
     {
          listC_Debt.Add(decimal.Parse(values[2]));
     }
     if (values[3] == "")
     {
          listD_Credit.Add(0);
     }
     else
     {
          listD_Credit.Add(decimal.Parse(values[3]));
     }
     if (values[4] == "")
     {
          listE_Balance.Add(0);
     }
     else
     {
          listE_Balance.Add(decimal.Parse(values[4]));
     }
}

while (a < listA_DateTime.Count)
{
     this.dataGridView1F2.Rows.Add(listA_DateTime[a].ToShortDateString(), listB_Location[a], listC_Debt[a], listD_Credit[a], listE_Balance[a]);
     a++;
}

Thanks in advance for any help that can be given.

Max
  • 12,622
  • 16
  • 73
  • 101
teepee
  • 309
  • 3
  • 5
  • 13
  • 1
    You should surround any file I/O, like `StreamReader`, with a `using` statment. E.g. `using(var stream = new StreamReader(filepath)) { // Read stuff }`. You forgot to close the stream, which can end up doing obnoxious things. The `using` statement will clean up for you, as soon as execution leaves its scope. – Keeler Jan 10 '14 at 06:11
  • I tried that but nothing changed. Thank you – teepee Jan 10 '14 at 06:26
  • That's not an implementation of your desired behavior, that's just a suggestion to improve your code. Otherwise I would have posted it as an answer. – Keeler Jan 10 '14 at 06:27
  • which part is showing your try to : "*..take the string from my ListBox on Form 1 and use it on Form 2*"? – har07 Jan 10 '14 at 06:54
  • see following question for reference [How to access a form control for another form?](http://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – har07 Jan 10 '14 at 06:56
  • 'var reader1 = new StreamReader(File.OpenRead(GetCsvFile1(frm1.pPath)));' at the end of the code, frm1.pPath - pPath is the string being used in the method to find the csv file – teepee Jan 10 '14 at 06:58
  • Check whenever `frm1` is really *instance* of the form you are using to find a directory (or file, or whatever). More likely you forgot passing frm1 to form 2. For the future, always *check* values by setting breakpoints (in your case, set breakpoint inside `GetCSVFile1`, and see for yourself, which value you get there). – Sinatr Jan 10 '14 at 08:56

0 Answers0