BlakeMcKenna
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(str Col)
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.AutoGenerateC olumns = 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
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(str
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
Next
dtLoadTest.Rows.Add(row)
Next
dgvLoadTests.AutoGenerateC
dgvLoadTests.DataSource = Nothing
dgvLoadTests.DataSource = dtLoadTest
Catch ex As Exception
EH.strRetVal = gfrmID & "/CreateLoadTestDataTable(
End Try
EH.ProcessMessages(Me, sbr, EH.strRetVal)
End Sub
Screenshot.jpg
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.
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?
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?
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That was the problem. I didn't assign the column name to the Grid Columns. Once I did that it loaded perfectly. Thanks "TheLearnedOne"!
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?