Solved

DataGridView not populating based on DataTable values

Posted on 2013-12-10
7
268 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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now