|
-
May 22nd, 2017, 10:33 PM
#1
Thread Starter
Lively Member
Loading CSV file into DataGridView missing a row | SOLVED
Hello! I am trying to load in a CSV file into DataGridView and it works but I am missing one row. I save 5 rows of data and I only get 4 in return. Here is the code(I am still trying to learn and do so by asking questions and reading about stuff):
Code:
Dim fName As String = ""
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.ShowDialog()
fName = OpenFileDialog1.FileName
Dim TextLine As String = ""
If IO.File.Exists(fName) Then
Dim Contents = IO.File.ReadAllLines(fName).ToList
Contents.RemoveAt(0)
Contents.RemoveAt(Contents.Count - 1)
Contents.RemoveAt(Contents.Count - 1)
For x As Integer = 0 To Contents.Count - 1
Contents(x) = Contents(x).Replace(",,,", ",").Replace(",,", ",")
Contents(x) = Contents(x).Substring(0, Contents(x).Count - 1)
Dim RowItem = Contents(x).Split(","c)
DataGridView1.Rows.Add(RowItem)
Next
End If
Thanks in Advance!
Last edited by Dragnorian; May 23rd, 2017 at 01:14 AM.
-
May 22nd, 2017, 10:48 PM
#2
Re: Loading CSV file into DataGridView missing a row
Debug your code. Place a breakpoint at the top and step through it line by line. Test all the relevant variables and expressions at each step to make sure that they evaluate to exactly what you expect. There are only two possible outcomes to that: either you find a place where reality does not match expectation or you don't. In the former case, you may well be able to solve the issue for yourself but, if you can't, you can at least give us all the details about what you found, including exactly where the issue occurred and how reality differed from expectation. You can also provide us with the actual data you used, so that we can test the code for ourselves if required. If you don't find an issue when debugging then that means that there's an issue with your expectations rather than your code, so you need to address that.
-
May 23rd, 2017, 01:13 AM
#3
Thread Starter
Lively Member
Re: Loading CSV file into DataGridView missing a row
Alright, so the error in this code is this(Will show original code):
Code:
Dim fName As String = ""
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.ShowDialog()
fName = OpenFileDialog1.FileName
Dim TextLine As String = ""
If IO.File.Exists(fName) Then
Dim Contents = IO.File.ReadAllLines(fName).ToList
Contents.RemoveAt(0)
Contents.RemoveAt(Contents.Count - 1)
Contents.RemoveAt(Contents.Count - 1)
For x As Integer = 0 To Contents.Count - 1
Contents(x) = Contents(x).Replace(",,,", ",").Replace(",,", ",")
Contents(x) = Contents(x).Substring(0, Contents(x).Count - 1)
Dim RowItem = Contents(x).Split(","c)
DataGridView1.Rows.Add(RowItem)
Next
End If
Where, in that code, there is code that is written twice that shouldn't. More specifically, this group of code:
Code:
Contents.RemoveAt(0)
Contents.RemoveAt(Contents.Count - 1)
Contents.RemoveAt(Contents.Count - 1)
If you comment or remove one of the: Contents.RemoveAt(Contents.Count - 1) then everything will work to plan. Guess little mistakes occur when just scanning through code and not paying attention when should haha. Thanks for helping me realize this mate!
-
May 23rd, 2017, 02:00 AM
#4
Re: Loading CSV file into DataGridView missing a row
 Originally Posted by Dragnorian
Guess little mistakes occur when just scanning through code and not paying attention when should
We all miss things at times when just reading code because we often see what we expect to see rather than what's actually there. That's why debugging is so important. If you watch your code in action then issues like that become obvious very quickly. Debugging is a critical skill and VS provides lots of tools for the purpose. It will often be faster than posting to a forum and waiting for someone else to take a look and you're also more likely to get help on a forum if you can provide more specific information, so it's a win-win.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|