Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Okay, so I'm coding in C# and just starting out, and my first assessment is to take data from a CSV file and load it onto a list (must use a list) and then load that into a dataGridView in my Windows Forms App and then have it be editable. Now I have a decent idea how to do it once I have it in the dataGridView but I'm seriously confused about this list not being accessible once I create and return it. Am I doing something seriously wrong when returning, or missing a step??

If I set a break-point right on returning the list, it works fine and my 40 lines of data from the CSV file are all arranged into their list items and properly sorted, but once I get out of that bit of code, it just doesn't show up.

Finally, my plan once I get the data into this dataTable is to set the source on my dataGridView to the dataTable as a bunch of online forum posts have said works find, and obviously I haven't written it to add all the different items yet but first I need to figure out why the list isn't showing up in intellisense or working at all. Thanks in advance.

public static IList<DailyValues> ReadValues(string path)
        {
            var myList = new List<DailyValues>();
            foreach (var line in File.ReadLines(path).Skip(1))
            {
                myList.Add(DailyValues.FromLine(line));
            }
            return myList;
        }

        public static DataTable dt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemCode");
            dt.Columns.Add("ItemDescription");
            dt.Columns.Add("CurrentCount");
            dt.Columns.Add("OnOrder");
            foreach (var ItemCode in myList)
            {
                dt.Rows.Add(new object[] { ItemCode.ItemCode });
            }
            return dt;
        }


What I have tried:

this is just the current iteration, I tried a bunch of stuff that completely didn't work.
Posted
Updated 10-Aug-17 19:58pm

Looking at your code sample, the myList variable is not in the scope of your dt() method. You need to pass the result from ReadValues(...) method and pass it to your dt() method ... something like
C#
public static DataTable dt(IList myList)
{
 // code goes here
}

Then to call:
C#
var table = dt(ConvertCSVtoDataTable(inFilePath));
 
Share this answer
 
Comments
BillWoodruff 11-Aug-17 0:55am    
+5 this is a solid, appropriate, solution.
Graeme_Grant 11-Aug-17 1:55am    
This was the missing link for him... ;)
Member 13356276 11-Aug-17 4:52am    
Oh my, thanks so much man, this helped me figure it out, and as the answers below also said, I need to look into variable scopes. Thank you all for the help regardless.
Here is a working piece of code that shows how to get a CSV file into a DataTable. It won't take much to alter to do what you want for your "first assessment".
C#
public static DataTable ConvertCSVtoDataTable(string strFilePath)
{
    DataTable dt = new DataTable();
    using (StreamReader sr = new StreamReader(strFilePath))
    {
        string[] headers = sr.ReadLine().Split(',');
        foreach (string header in headers)
        {
            dt.Columns.Add(header);
        }
        while (!sr.EndOfStream)
        {
            string[] rows = sr.ReadLine().Split(',');
            DataRow dr = dt.NewRow();
            for (int i = 0; i < headers.Length; i++)
            {
                dr[i] = rows[i];
            }
            dt.Rows.Add(dr);
        }
    }
    return dt;
}
 
Share this answer
 
Comments
BillWoodruff 11-Aug-17 1:01am    
My vote of #1: by completing the OP's assignment for them, with a second solution they did not ask for, you are taking away from them the opportunity to solve the problem for themselves, and undercutting whatever educational process they are in ... the OP here expresses confidence they are able to take that next step.
Graeme_Grant 11-Aug-17 1:55am    
a #1 is a bit harsh Bill considering that it is working code... This is not an exact response, it still needed a lot of refactoring to get what he needed.

He was close... This is just an example of something that works. Google search would have given him something almost identical...
Quote:
C# - list created in part of code can't be accessed from the rest

You need to learn 'Variables scopes'. Depending on what you want to do, the place where you declare a variable matters.
3.7 Scopes (C#)[^]

"A more formal definition is that scope is an enclosing context or region that defines where a name can be used without qualification."
Understanding Classes and Objects the C# Way | Object-Oriented Programming | InformIT[^]

Visual C# 2010 - Variable Scope - YouTube[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900