Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGridView not populating based on DataTable values

Posted on 2013-12-10
7
Medium Priority
?
291 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 27

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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