Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

DataGridView not populating based on DataTable values

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
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

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?
Avatar of BlakeMcKenna

ASKER

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.
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.
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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was the problem. I didn't assign the column name to the Grid Columns. Once I did that it loaded perfectly. Thanks "TheLearnedOne"!