Solved

DataGridView not populating based on DataTable values

Posted on 2013-12-10
7
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

617 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