troubleshooting Question

ICollection method does not work as expected

Avatar of Paulagier
PaulagierFlag for France asked on
.NET ProgrammingC#
5 Comments1 Solution71 ViewsLast Modified:
Using #, WPF

Starting from a text file I need to:
1. Import this text file into a Data Table
2. Browse the data rows and in some case add an extra row
3. Convert this DT to a List
4. Display either the list or the DT in a datagrid (via a button click)

My code works well in step 1 and 4 but skip the 2 and 3
Unable to find why steps 2 and 3 are ignored

Here comes the C# method
 private ICollection CreateDataSource_vNodes() // vNodes
        {
            //Create new DataTables and Rows
            DataTable dt_vNodes = new DataTable();
            DataRow dr;

            // Create columns with headers
            dt_vNodes.Columns.Add("IdNumber", typeof(int));
            dt_vNodes.Columns.Add("Position", typeof(string));

            //Read the file vNodes_Capture.txt and, for each line, create a new row in dt_vNodes
            string txtDataBase = "vNodes_Capture.txt";

            foreach (string Line in File.ReadLines(txtDataBase))
            {
                //Split lines at delimiter ';''
                //Create new Row
                dr = dt_vNodes.NewRow();

                //IdNumber =
                dr[0] = Line.Split(';').ElementAt(0);

                //Position =
                dr[1] = Line.Split(';').ElementAt(1);

                //Add the row we created
                dt_vNodes.Rows.Add(dr);
            }

            // Check values in column "Position" and add virtual bones if necessary    

            List<DataRow> rows = dt_vNodes.AsEnumerable().ToList();
            foreach (DataRow drow in rows)
            {
                int currentidnumber = Convert.ToInt32(drow["IdNumber"]);
                string currentposition = drow["Position"].ToString();
                if (currentposition.ToLower() == "upspine") // convertie en minuscules
                {
                    dt_vNodes.Rows.Remove(drow); // delete row with upspine
                    dt_vNodes.Rows.Add(new object[] { currentidnumber, "LeftShoulder" });
                    dt_vNodes.Rows.Add(new object[] { currentidnumber, "RightShoulder" });
                }
                else if (currentposition.ToLower() == "pelvis") //convertie en minuscule
                {
                    dt_vNodes.Rows.Remove(drow); // delete row with pelvis 
                    dt_vNodes.Rows.Add(new object[] { currentidnumber, "LeftHip" });
                    dt_vNodes.Rows.Add(new object[] { currentidnumber, "RightHip" });
                }
                else
                {
                    dt_vNodes.Rows.Add(new object[] { currentidnumber, currentposition });
                }

            }

            // Converting the DT in a List<string> for further use

            foreach (DataRow drow in rows)
            {
                BonesRealVirtualS.Add(drow["Position"].ToString());
            }

            //Return Dataview that populates the datagrid ItemsSource
            DataView dvN = new DataView(dt_vNodes);
            dgBB.ItemsSource = BonesRealVirtualS;
            return dvN;

        }

and the xaml line
<DataGrid Name="dgbonesRV" HorizontalAlignment="Left" Margin="41,64,0,0" VerticalAlignment="Top" Height="214" Width="194"/>

text file is attached
vNodes_Capture.txt
ASKER CERTIFIED SOLUTION
Paulagier
Research Director

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros