I have a datagrid that is populated with data. If the operator scrolls off to the click on a field that is off to the right (not initially displayed on the grid) ---- then the operator does something that needs to repopulate that data with a different set of information --- an error is generated. The new data consists of the same fields, but just it is just pulling in a different set of data. If the operator does not scroll off to the side of the grid but refreshes the grid with new information, the error does not occur. If the operator scrolls off to the right, but does not click on a cell, an error does not occur. I have included the code that binds data to the grid and displays it. I've narrowed down that it is stopping on the "dgtabhistory.DataSource = mydataview " statement where I bind the data to the view. The error says: "An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Column 'Strategy ID and Desc' does not belong to table ACTIVITYHISTORYGRID." The column name may change --- it kind of depends on how far over to the right I scroll on the grid and which cell I click on.

Does anyone know about a possible bug with VB.net? Or am I missing something?? Is there a special command(s) I need to do before I bind new data to a datagrid?

Dim sqlConn4 As SqlConnection = New SqlConnection(Connection)

            Try

                sqlConn4.Open()
                Dim sqlcomm4 As New SqlDataAdapter("salesRaptor_GetActivityHistoryPC", sqlConn4)
                sqlcomm4.SelectCommand.CommandType = CommandType.StoredProcedure

                Dim workParam4 As SqlParameter = New SqlParameter
                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@Rep", SqlDbType.VarChar, 25)
                If chkTabHistoryWholeTeam.CheckState = CheckState.Checked Then
                    sqlcomm4.SelectCommand.Parameters("@Rep").Value = Rep_ID
                Else
                    sqlcomm4.SelectCommand.Parameters("@Rep").Value = txTabHistoryRepID.Text
                End If
                workParam4.Direction = ParameterDirection.Input

                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@Status", SqlDbType.VarChar, 25)
                Dim type As String
                type = ""
                If cmbTabHistoryStatus.SelectedItem = "All Closed Statuses" Then type = ""
                If cmbTabHistoryStatus.SelectedItem = "Emailed Status Only" Then type = "E"
                If cmbTabHistoryStatus.SelectedItem = "Attempted Status Only" Then type = "A"
                If cmbTabHistoryStatus.SelectedItem = "Completed Status Only" Then type = "C"
                sqlcomm4.SelectCommand.Parameters("@Status").Value = type.ToString
                workParam4.Direction = ParameterDirection.Input

                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@ProspectID", SqlDbType.VarChar, 25)
                sqlcomm4.SelectCommand.Parameters("@ProspectID").Value = Prospid.ToString
                workParam4.Direction = ParameterDirection.Input

                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@contactID", SqlDbType.VarChar, 25)
                If tabhistproscontsw = "CONTACT" Then
                    sqlcomm4.SelectCommand.Parameters("@contactid").Value = ActContid.ToString
                Else
                    sqlcomm4.SelectCommand.Parameters("@contactid").Value = ""
                End If
                workParam4.Direction = ParameterDirection.Input

                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@wholeteam", SqlDbType.VarChar, 1)
                If chkTabHistoryWholeTeam.Checked = True Then
                    sqlcomm4.SelectCommand.Parameters("@wholeteam").Value = "Y"
                Else
                    sqlcomm4.SelectCommand.Parameters("@wholeteam").Value = "N"
                End If
                workParam4.Direction = ParameterDirection.Input

                workParam4 = sqlcomm4.SelectCommand.Parameters.Add("@allreps", SqlDbType.VarChar, 1)
                If chkTabHistoryAllReps.CheckState = CheckState.Checked Then
                    sqlcomm4.SelectCommand.Parameters("@allreps").Value = "Y"
                Else
                    sqlcomm4.SelectCommand.Parameters("@allreps").Value = "N"
                End If
                workParam4.Direction = ParameterDirection.Input

                Dim ds As New DataSet
                sqlcomm4.Fill(ds, "ACTIVITYHISTORYGRID")     'holds data from query

                'ds.Tables(0).Columns(16).ColumnMapping = MappingType.Hidden ' hide the record status field

                Dim x = ds.Tables(0).Rows.Count()
                Dim mydatatable As DataTable = ds.Tables(0) 'table name
                Dim mydataview As DataView = mydatatable.DefaultView
                ' mydataview.Sort = "Date Closed"  'sort table
                dgtabhistory.DataSource = mydataview 'binds data to grid

                Dim ts1 As New DataGridTableStyle ' change column widths
                ts1.MappingName = "ACTIVITYHISTORYGRID"

                Dim numRows As Integer = dgtabhistory.BindingContext(dgtabhistory.DataSource, dgtabhistory.DataMember).Count
                txRecCountHistory.Text = "Count: " + CStr(numRows.ToString)
                If (dgtabhistory.CurrentRowIndex < numRows) And (dgtabhistory.CurrentRowIndex > -1) Then
                    ContactHistoryActivityID = CStr(dgtabhistory(dgtabhistory.CurrentRowIndex, 13).ToString) 'store current activity id
                End If

                Try             ' put inside a try because 2nd time through, it errors out
                    dgtabhistory.TableStyles.Add(ts1)

                    ts1.GridColumnStyles(0).Width = 120 ' change width of date/time fld
                    ts1.GridColumnStyles(1).Width = 150 ' change width of contact name
                    ts1.GridColumnStyles(2).Width = 120 ' change width of status
                    ts1.GridColumnStyles(3).Width = 120 ' change width of type
                    ts1.GridColumnStyles(4).Width = 200 ' change width of regarding
                    ts1.GridColumnStyles(5).Width = 200 ' change width of results
                    ts1.GridColumnStyles(6).Width = 50 ' change width of priority
                    ts1.GridColumnStyles(7).Width = 100 ' change width of rep
                    ts1.GridColumnStyles(8).Width = 100 ' change width of personal business
                    ts1.GridColumnStyles(9).Width = 150 ' change width of strategy id/des
                    ts1.GridColumnStyles(10).Width = 90 ' change width of strategy level
                    ts1.GridColumnStyles(11).Width = 75 ' change width of order id
                    ts1.GridColumnStyles(12).Width = 75 ' change width of prospect id
                    ts1.GridColumnStyles(13).Width = 75 ' change width of activity id
                    ts1.GridColumnStyles(14).Width = 75 ' change width of contact id
                    ts1.GridColumnStyles(15).Width = 150 ' change width of name
                    ts1.GridColumnStyles(16).Width = 75 ' change width of new prospect

                    ts1.DataGrid.Refresh()

                    ' date datetime fields to show time    
                    Dim dgtbc As DataGridTextBoxColumn
                    dgtbc = CType(dgtabhistory.TableStyles(0).GridColumnStyles(0), DataGridTextBoxColumn)
                    If Not dgtbc Is Nothing Then dgtbc.Format = "g"


                Catch

                End Try

                sqlConn4.Close()

            Catch tt As Exception
                Errmsg = tt.ToString
                EventSW = "Y"
                errormsg() ' display error msg

                sqlConn4.Close()

            Finally
                sqlConn4.Close()

            End Try

I found the problem. I had to add dgtabhistory.DataSource = nothing before the dgtabhistory.DataSource = mydataview.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.