dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    14

    Populating DataGrid works but adds many empty rows

    Hi all,

    I'm not sure how to explain this better so here goes:

    I have an unbounded DataGrid on a form which I partly populate with data from a CSV file. The code below works well enough (I know it's simplistic) but what happens is that once it's done, there's a lot of empty rows at the bottom of the data. My guess is that the sub keeps adding empty rows for some reason until it reaches maximum rows on the DataGrid.

    Code:
        Private Sub BtPopulate_Click(sender As Object, e As EventArgs) Handles BtPopulate.Click
            Using AlgoRead As New Microsoft.VisualBasic.
               FileIO.TextFieldParser(
                 "W:\WaR\Work\Programming\Visual Basic\CoalWorks AIO\CoalWorks AIO\Data\Miners.csv")
                AlgoRead.TextFieldType = FileIO.FieldType.Delimited
                AlgoRead.SetDelimiters(",")
                Dim currentRow As String()
                Dim CurrentRowCount As Integer = 1
    
                While Not AlgoRead.EndOfData
                    Try
                        currentRow = AlgoRead.ReadFields()
                        DGAlgoWallets.Rows.Add(CurrentRowCount)
                        DGAlgoWallets.Item(2, CurrentRowCount - 1).Value = currentRow(0)
                        DGAlgoWallets.Item(3, CurrentRowCount - 1).Value = currentRow(1)
                        DGAlgoWallets.Item(1, CurrentRowCount - 1).Value = False
                        CurrentRowCount = CurrentRowCount + 1
                    Catch ex As Microsoft.VisualBasic.
                                FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message &
                        "is not valid and will be skipped.")
                    End Try
                End While
            End Using
        End Sub
    The data in columns 0 and 4 is ignored. Column 0 contains icons and I haven't created those yet, and column 4 contains strings which the application user adds as needed.

    Of course, the intended purpose is for the DataGrid to have exactly the amount of rows that there is data for in the file (currently 57 rows). I have no clue as to why it keeps adding many empty rows.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    335

    Re: Populating DataGrid works but adds many empty rows

    Nothing stands out as being wrong with the code, so then the file becomes the next thing to look at, which only you have access to at this point.

    If you open the .csv file in Notepad and scroll to the bottom, can you keep scrolling the cursor past the point that your data appears (meaning there are extra CRLF characters at the end)? Or are there rows of empty commas?

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,898

    Re: Populating DataGrid works but adds many empty rows

    Actually, something does stand out as being wrong with the code. Assuming that what you actually have is a DataGridView rather than a DataGrid, this is wrong:
    Code:
    DGAlgoWallets.Rows.Add(CurrentRowCount)
    That method adds the number of rows that you specify. That means that on the first iteration of the loop it will add one row, then it will add two more on the second iteration, then another three on the third iteration and so on. Presumably you only want to add one row to the grid each time you read a record so I'd suggest that that's what you should actually do.

    This is a fine example of why you need to learn how to debug your code. Far more often than not, you don't work out what's wrong with code simply by reading it. The better you get at doing so, the more complex the code you tend to write will be and you're still back where you started. You need to actually run the code and watch it in action. That's debugging. If you had done that then you'd have seen exactly what was happening to the number of rows in the grid each time you called that Add method. I suggest that you stop writing code right now and learn how to debug, i.e. set breakpoints, step through code and watch variables and other expressions as you step. Once you can do that, start writing code again with the knowledge that you'll be able to find the cause of an issue like this for yourself with a couple of minutes debugging. Even if you don't know how to fix the issue, at least you'll be able to provide us with far more relevant information so it's less likely that we'll miss things like this that are obvious when debugging.

    By the way, please use the proper names for things. If you're using a DataGridView, which is pretty much certain based on your code, then don't say that you're using a DataGrid. There are a number of controls that are called DataGrid so you've needlessly created the possibility of confusion. It's not hard to use the actual names for things and doing so reduces the possibility of confusion and people wasting their time as a result.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    14

    Re: Populating DataGrid works but adds many empty rows

    @jmcilhinney - thank you for your advice. I'm a beginner. The last time I used VB (without the A) was many, many years ago when VB6 was a new thing. I'm trying to start coding again, as an amateur, which we all were at some point in time.

    In my narrow IT domain (which is not development, by the way) I'm considered a very good professional and I am well aware of how annoying beginners can be. They don't get the exact names right, they don't fully explain the problem they're facing, and I have to extract info from them bit by bit. At the same time I am aware that, if nurtured well, most of them will get over that phase quicker than if I cuss them.

    I admit I didn't know there was a DataGrid and a DataGridView - I'm self-taught, barely starting. Now I do know, and I'll remember in the future. I also know how to set breakpoints and debug my code, but still far from considering myself an expert. Yet another area where I'm a beginner. Back in 2008-2009 I had written a couple projects in VBA, alone, from scratch and had refactored my code several times, improving it with every iteration. They're still being used right now in my company. So hopefully that rules me out from being a total idiot and puts me squarely in the "newbie" group. We've all been there. Some of us might have forgotten, though.

    I had read the definition of ".rows.add" but admittedly I didn't understand it well enough. There are many reasons for this: English is not my native language, it was 2 AM my time (I can only code for 1-2h a day, in the middle of the night, after finishing my 2nd job) and I was tired as a dog. Mistakes have been made, I apologize.

    With that being said, you were spot-on, that's what was causing the issue, thank you again for you (rather strongly-worded) advice.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,127

    Re: Populating DataGrid works but adds many empty rows

    That's the debugging thing, though. I understand why JMC was hammering on about debugging, because the number of people who come in here never having learned how to set a breakpoint or step through code is amazing to me. I feel it's the second thing that should ever be taught in a class. I learned about breakpoints the easy way: A cat jumped on my keyboard and set a breakpoint. By the time I had that fixed, I knew all about it.

    But not everybody has a cat handy. Dogs just aren't any good at this kind of thing, and hamsters are right out! So, if you lack the benefits of a convenient cat, then you have to learn breakpoints and stepping somewhere. Simple mistakes are the hardest to catch. You never seem to see the easy ones when looking at your own code. After all, you already know what you think it should be doing, so when it doesn't do that, the only thing to do is step through the code seeing whether it was doing what you expected. You were getting too many rows, so you knew roughly what to look at: Anything that added a row. So, you could go through the loop one time and see how many rows you had. It would be 1. That would be right. You'd then go through the loop a second time and see how many rows you had. It would be 3....that wouldn't be so right.

    Of course, in this case, I'd be suspecting the file first, because I've seen "extra" lines get added to the end of a file too many times. Excel can do some fun things to CSV files, for example. That's what OptionBase1 was getting at, and it would certainly be the first thing I'd be looking at, too. However, once I saw that it wasn't the file, then the next step would be to step my way through the loop a couple times, and that would have done it.
    My usual boring signature: Nothing

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
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.