Solved

DataGridView not populating based on DataTable values

Posted on 2013-12-10
7
275 Views
Last Modified: 2013-12-11
I have a DataGridView that isn't populating correctly. I basically load a datagridview with data. Then, I execute a procedure that creates a DataTable based on the dgv and it's data.

Once the DataTable is built with the data, I clear the dgv, and then assign the DataTable as it's DataSource. The correct amount of rows are present but the cells are empty. See the attached screenshot. My code is below where I create the DataTable and assign it as the DGV's datasource.


        Private Sub CreateLoadTestDataTable()
        Try
            Dim colCnt As Integer = dgvLoadTests.Columns.Count
            Dim strCol As String = ""

            dtLoadTest = New DataTable

            For x As Integer = 0 To colCnt - 1
                strCol = "Col"
                strCol &= x + 1
                dtLoadTest.Columns.Add(strCol)
            Next

            For x As Integer = 0 To rowCnt - 1
                Dim row As DataRow
                row = dtLoadTest.NewRow

                For y As Integer = 0 To colCnt - 1
                    strCol = "Col"
                    strCol &= y + 1
                    row(strCol) = dgvLoadTests.Rows(x).Cells(y).Value
                Next

                dtLoadTest.Rows.Add(row)
            Next

            dgvLoadTests.AutoGenerateColumns = False
            dgvLoadTests.DataSource = Nothing
            dgvLoadTests.DataSource = dtLoadTest

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/CreateLoadTestDataTable() - " & ex.Message
        End Try

        EH.ProcessMessages(Me, sbr, EH.strRetVal)
    End Sub
Screenshot.jpg
0
Comment
Question by:BlakeMcKenna
  • 3
  • 2
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39711310
Does your datagridview load correctly without this routine?
Instead of loading your datagridview first, could you instead load your source data into a DataTable and then attach it to your datagridview?

BTW, where do you define RowCnt?
0
 

Author Comment

by:BlakeMcKenna
ID: 39711325
On the initial load, it is loaded manually. There is no DataSource involved on the initial load.

The data in the 1st column (Serial#) and the values going across in line 0 are all done based on input elsewhere in the app.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39711361
I would think that the columns don't match the properties that the DataGridView expects, since you are generating generic column names with "Col" plus a number.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39711383
As rowCnt isn't defined in this code block, this would appear to be a global variable. Where do you assign it a value?

Have you attempted to debug the code to determine that dtLoadTest is being populated correctly?

If the first column and first row are populated based on events elsewhere in the application, how do the other values get generated? Could the issue lie elsewhere within the application?
0
 

Author Comment

by:BlakeMcKenna
ID: 39711396
I have tried debugging it and based on Debug.Print statements as the Table is being loaded into the grid, the value do appear. That's why it doesn't make sense. I think that what "TheLearnedOne" mentioned might be a possible reason. I'll explore that a little more.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39711402
Since you are not using auto-generated columns, the column names need to match the DataPropertyName for each column.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39711427
That was the problem. I didn't assign the column name to the Grid Columns. Once I did that it loaded perfectly. Thanks "TheLearnedOne"!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question