Avatar of Darwin Johnson
Darwin JohnsonFlag for United States of America

asked on 

Datagridview only showing last row's data

I am trying to populate a datagridview from a datatable but I don't want to bind it to the datatable. With the below code it's only displaying data in the last row (see attached picture). There are 5 records in the table. When I single step through the code and watch the row count is changing and the data is changing.

        Dim CheckUser As DataTable, intRow As Integer = 0

        CheckUser = ReadDataFromDB("SELECT * FROM users;", False)

        For Each row In CheckUser.Rows

            If Not String.IsNullOrEmpty(row("UserID").ToString) Then
                'DGridVUsers.CurrentRow.Cells(0).Value = row("UserID").ToString
                ' DGridVUsers.Rows(intRow).Cells(0).Value = row("UserID").ToString
                With DGridVUsers
                    .Rows(.Rows.Count - 1).Cells("UserID").Value = row("UserID").ToString
                    .Rows.Add()
                End With
            End If
            intRow += 1
        NextUser generated image
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)
Avatar of Arana (G.P.)
Arana (G.P.)

what h appens when you start your loop? when DBGridVUsers has 0 rows and you do:

.Rows(.Rows.Count - 1).Cells("UserID").Value = row("UserID").ToString

getting ROW(-1) the value of your first data row?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Your code looks strange.

Is this what you intended?
                With DGridVUsers
                   .Rows.Add()
                    .Rows(.Rows.Count - 1).Cells("UserID").Value = row("UserID").ToString
    '                .Rows.Add()
                End With
Avatar of Darwin Johnson

ASKER

When I load the form it shows row count of 1 before I do anything. So I get Row(0)
Avatar of Darwin Johnson

ASKER

Same result only data in last row

With DGridVUsers
                    .Rows.Add()
                    .Rows(.Rows.Count - 1).Cells("UserID").Value = row("UserID").ToString

                End With
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Darwin Johnson

ASKER

Thanks, for the help
Strange that nobody asked to see the code in ReadDataFromDB. The problem is quite probably there.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo