Link to home
Start Free TrialLog in
Avatar of ict-torquilclark
ict-torquilclark

asked on

why is this code adding two columns?

I am using the attatched code to add a comboboxColumn to a datagridview

can anybody suggest why it might be adding the column twice?
'Adds Inactive Reason Conbobox's 
        sqlStr = "SELECT * FROM Inactive"
        rs.Open(sqlStr, sqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
        Dim dgvCol As New DataGridViewComboBoxColumn
        Do While x < rs.RecordCount
            dgvCol.Items.Add(rs.Fields("Description").Value)
            x = x + 1
            rs.MoveNext()
        Loop
        rs.Close()
        datagridviewMembers.Columns.Add(dgvCol)

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Show us the full code block including delaration of the column and in which event the above code is.
Avatar of ict-torquilclark
ict-torquilclark

ASKER

this is the full code (it is a function)
Private Function LoadMembers(ByVal EmployerRef As String, ByVal OnlyShowActiveMembers As Boolean)

        datagridviewMembers.Rows.Clear()

        Dim rs As New ADODB.Recordset
        Dim sqlStr As String
        Dim x As Integer = 0
        rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient

        'Adds Inactive Reason Conbobox's 
        sqlStr = "SELECT * FROM Inactive"
        rs.Open(sqlStr, sqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
        Dim dgvCol As New DataGridViewComboBoxColumn
        Do While x < rs.RecordCount
            dgvCol.Items.Add(rs.Fields("Description").Value)
            x = x + 1
            rs.MoveNext()
        Loop
        rs.Close()
        datagridviewMembers.Columns.Add(dgvCol)

        x = 0
        sqlStr = "SELECT ClientRef,EmployerRef,Category,Title,Forename,Surname,DOB,DateJoinedScheme,NiNumber,Sex,Salary,SalsacAsPercentOfSalary,SalsacAsFigure,Active,InactiveRef FROM Clients WHERE EmployerRef = '" & EmployerRef & "' AND Active IN ('TRUE','" & OnlyShowActiveMembers & "') ORDER BY Surname ASC"
        rs.Open(sqlStr, sqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)

        Do While x < rs.RecordCount
            Dim dgvRow As New DataGridViewRow
            Dim dgvCell As DataGridViewCell

            'Adds ClientRef to hidden field
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("ClientRef").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Title to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("Title").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Forname to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("Forename").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Surname to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("Surname").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Sex to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("Sex").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds DOB to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("DOB").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds DJS to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("DateJoinedScheme").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds NINO to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("NiNumber").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Category to DGV
            Dim rs2 As New ADODB.Recordset
            rs2.CursorLocation = ADODB.CursorLocationEnum.adUseClient
            sqlStr = "SELECT PensionCategoryName FROM PensionCategories WHERE PensionCategoryRef = '" & rs.Fields("Category").Value & "'"
            rs2.Open(sqlStr, sqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs2.Fields("PensionCategoryName").Value
            dgvRow.Cells.Add(dgvCell)
            rs2.Close()

            'Adds Salary to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("Salary").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Salsac% to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("SalsacAsPercentOfSalary").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Salsac£ to DGV
            dgvCell = New DataGridViewTextBoxCell
            dgvCell.Value = rs.Fields("SalsacAsFigure").Value
            dgvRow.Cells.Add(dgvCell)

            'Adds Active to DGV
            dgvCell = New DataGridViewCheckBoxCell
            dgvCell.Value = rs.Fields("Active").Value
            dgvRow.Cells.Add(dgvCell)



            If IsDBNull(rs.Fields("InactiveRef").Value) = False Then
                sqlStr = "SELECT Description FROM Inactive WHERE InactiveRef = '" & rs.Fields("InactiveRef").Value & "'"
                rs2.Open(sqlStr, sqlCnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
                'MsgBox(rs2.Fields("Description").Value
                rs2.Close()
            End If

            'Adds Row to DGV
            datagridviewMembers.Rows.Add(dgvRow)
            x = x + 1

            rs.MoveNext()
        Loop

        MsgBox(datagridviewMembers.Columns.Count)

        datagridviewMembers.AutoResizeColumns()
        rs.Close()

    End Function

Open in new window

Well make sure you call the function only once. Where are you calling it?
aah that could be it

i am calling it in a tabpage_enter event

i think it may be being called when the form loads and then again when I open the tab :(

how can i stop it from adding a new column everytime i enter the tab?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial