Question Datagridview problem no columns

sylo_yg

New member
Joined
Jun 29, 2010
Messages
3
Programming Experience
5-10
Hi,

I have a puzzling stituation....I have a form with a tab control. On the first page of the tab control I have a datagridview working properly with 4 columns created at design time and populated at run time. On the second page of the tab control I have another datagridview with 11 columns created at design time but when I try to populate it (like I did for the first) as follows:
...
holdgv = New DataGridViewRow
holdgv.CreateCells(dgvRenHistory)
holdgv.Cells(dgvRenHistory.Columns("renId").Index).Value = ....some value
holdgv.Cells(dgvRenHistory.Columns("renPrevId").Index).Value = ....some value
...
dgvRenHistory.Rows.Add(holdgv)


I realize that dgvRenHistory.columns.count = 0 eventhough I created it with 11 columns...so what's the problem
 
CreateCells does not create column in the DataGridView, if that was what you thought. It initializes cell styles in DataGridViewRow based on the column info in grid.
To add columns to DataGridView you can use Columns property to get the columns collection, and then one of the Add methods to add a column. dgv.Columns.Add(...)
 
Hi JohnH and thanks for replying..............
I got what you said and to be clear I first created the datagridview at design time with around 11 columns and in the code I had the following


'For num = 0 To tabhist.Rows.Count - 1
'holdgv = New DataGridViewRow

' holdgv.CreateCells(dgvRenHistory)

' holdgv.Cells(dgvRenHistory.Columns("renId").Index).Value = tabhist.Rows.Item(num)(0)
' holdgv.Cells(dgvRenHistory.Columns("renPrevId").Index).Value = tabhist.Rows(num)(5)
' holdgv.Cells(dgvRenHistory.Columns("renPrevYear").Index).Value = tabhist.Rows(num)(2)
' Select Case tabhist.Rows(num)(3)
' Case 1
' holdgv.Cells(dgvRenHistory.Columns("renPrevSem").Index).Value = "SPRING"
' Case 2
' holdgv.Cells(dgvRenHistory.Columns("renPrevSem").Index).Value = "SUMMER"
' Case 4
' holdgv.Cells(dgvRenHistory.Columns("renPrevSem").Index).Value = "FALL"
' End Select
' holdgv.Cells(dgvRenHistory.Columns("renPrevSess").Index).Value = tabhist.Rows(num)(4)

' If Not IsDBNull(tabhist.Rows(num)(8)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj11").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(8))
' End If
' If Not IsDBNull(tabhist.Rows(num)(9)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj12").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(9))
' End If
' If Not IsDBNull(tabhist.Rows(num)(10)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj13").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(10))
' End If
' If Not IsDBNull(tabhist.Rows(num)(11)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj21").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(11))
' End If
' If Not IsDBNull(tabhist.Rows(num)(12)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj22").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(12))
' End If
' If Not IsDBNull(tabhist.Rows(num)(13)) Then
' holdgv.Cells(dgvRenHistory.Columns("renPrevMaj23").Index).Value = admAppSvrObj.getMajTitle(tabhist.Rows(num)(13))
' End If
' dgvRenHistory.Rows.Add(holdgv)
'Next

The funny thing is that this worked a couple of times at first and then the application crashed so I deleted the form and recreated it but I had the same result. Now the problem is actually with holdgv.CreateCells(dgvRenHistory) because eventhough I have 11 columns in that datagridview if I do dgvRenHistory.columns.count it gives 0...and what's more awkward is that in form load if I check it it gives 11.....and even more I have another datagridview on the same form but in another tab page which have similar code and working fine.................I hope I wasn't too long here and thanks in advance for your help.
 
Back
Top