Solved

Datagridview index out of range after form reload

Posted on 2011-02-21
9
475 Views
Last Modified: 2012-05-11
I have 2 datagridviews on a form. They look almost exactly alike except one is a "Manager View" and the other is a "National View". Manager View shows data data for all his employees. National View shows all the manager's data. In national view, if I click the managers name, it shows manager view with his emplyees. If I click an employee in Manager view, it closes the form and displays the detail for the employee. Here's the weird problem...... If I never go to national view I can click an employee to display his details, come back and do it again without issue. However, if I display national view; click a manager; then click and employee to display the emplyee's detail (it closes the form); then open the form again, I get an index out of range trying to modify column header text. The grid that is supposed to diplay first during form load is the Manager View. The manager view has Zero columns at this point (albeit the reason I'm getting the index out of range). Why would there be no columns? I didn't remove them. I'm creating a New form (Dim F as SomeForm = New SomeForm) when I re-display the Manager/National View form.
0
Comment
Question by:Misledman
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:rgn2121
Comment Utility
First off....this is very difficult to read, which is why I would guess you haven't gotten rsponses.

Second, is there code that we can view?

Also, are you clicking the names in one grid and it is filtering the other grid?  Like clicking the Manager name in Grid 1 displays the manager view in grid 2?
0
 
LVL 1

Author Comment

by:Misledman
Comment Utility
The national grid has a list of managers. Clicking the manager name hides that grid then displays the otyher grid that has a list of that manager's employees. If you click an employee from that grid, it closes the form and displays employee detail.
0
 
LVL 12

Expert Comment

by:rgn2121
Comment Utility
Can you post the code that you use to open the employee details from the National view?
0
 
LVL 1

Author Comment

by:Misledman
Comment Utility
grdProspect is the Manager's view grid. grdnational is the national view grid. I have resolved the problem by hiding rather than closing the form.See this code in grdProspect:
If e.ColumnIndex = 0 And e.RowIndex > -1 Then
            Cursor.Current = Cursors.WaitCursor
            frmDashBoard.cboProsEmp.SelectedValue = grdProspect.Rows(e.RowIndex).Cells("EmployeeID").Value
            frmDashBoard.cboPromoID.SelectedValue = cboPromo.SelectedValue
            frmDashBoard.Show()
            Me.Hide()
            Cursor.Current = Cursors.Default
            Exit Sub
        End If


Sorry for the delay in following up on this. I've been out of town a few weeks.
Private Sub FillNationalGrid()
        Cursor.Current = Cursors.WaitCursor
        If cboQuestion.SelectedIndex > 0 Then
            Select Case mQuestions(cboQuestion.SelectedIndex).DataType
                Case 0
                    mNational = NationalSummaryList.GetNationalList(mPromoID, cboEndDate.Value, mListType, txtAnswer.Text, mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)
                Case 1
                    mNational = NationalSummaryList.GetNationalList(mPromoID, cboEndDate.Value, mListType, cboAnswer.SelectedValue, mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)

                Case 2
                    mNational = NationalSummaryList.GetNationalList(mPromoID, cboEndDate.Value, mListType, IIf(chkAnswer.Checked, "1", "0"), mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)

            End Select
        Else

            mNational = NationalSummaryList.GetNationalList(mPromoID, cboEndDate.Value, mListType, "", 0, 0)

        End If

        grdNational.DataSource = mNational
        txtProsActualDollars.Text = mNational.ActualDollars
        txtProsContactGoal.Text = mNational.ContactGoalColumnText
        txtProsContactGoalAcheived.Text = mNational.ContactGoalText
        txtProsContact.Text = mNational.ContactCount '& " - " & mNational.ContactPercent
        txtProsCount.Text = mNational.TotalCount
        txtProsCount2.Text = mNational.TotalCount
        txtProsSoldCount.Text = mNational.SoldCount & " (" & mNational.SoldPercent & ")"
        txtProsPotentialDollars.Text = mNational.QuoteDollars
        txtProsProspectGoal.Text = mNational.ProspectGoalAcheived
        txtProsProspectGoalAcheived.Text = mNational.ProspectGoalText
        txtProsSalesGoal.Text = mNational.SalesGoalColumnText
        txtProsSalesGoalAcheived.Text = mNational.SalesGoalText
        txtProsStat1.Text = mNational.Status1
        txtProsStat2.Text = mNational.Status2
        txtProsStat3.Text = mNational.Status3
        txtProsStat4.Text = mNational.Status4
        txtProsStat5.Text = mNational.Status5
        txtProsStat6.Text = mNational.Status6
        txtProsStat7.Text = mNational.Status7
        txtProsStat8.Text = mNational.Status8
        ShowLabels()
        mStatusList = StatusListAll.GetList(mPromoID, False)
        mStatusListShort = StatusListAll.GetList(mPromoID, True)
        Dim I As Int32
        For I = 0 To grdNational.Columns.Count - 1
            Debug.Print(I & " " & grdNational.Columns(I).Name)
        Next
        For I = 1 To mStatusList.Count - 1
            Select Case I
                Case 1
                    lblProsStat1.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(8).HeaderText = mStatusListShort(I).Value
                Case 2
                    lblProsStat2.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(9).HeaderText = mStatusListShort(I).Value
                Case 3
                    lblProsStat3.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(10).HeaderText = mStatusListShort(I).Value
                Case 4
                    lblProsStat4.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(11).HeaderText = mStatusListShort(I).Value
                Case 5
                    lblProsStat5.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(12).HeaderText = mStatusListShort(I).Value
                Case 6
                    Debug.Print(grdNational.Columns(13).HeaderText)
                    lblProsStat6.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(13).HeaderText = mStatusListShort(I).Value
                    Debug.Print(grdNational.Columns(13).HeaderText)
                Case 7
                    lblProsStat7.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(14).HeaderText = mStatusListShort(I).Value
                Case 8
                    lblProsStat8.Text = mStatusList(I).Value & ":"
                    grdNational.Columns(15).HeaderText = mStatusListShort(I).Value
            End Select
        Next
        If mStatusList.Count <= 8 Then
            For I = mStatusList.Count To 8
                Select Case I
                    Case 1
                    Case 2
                    Case 3
                    Case 4
                        grdNational.Columns(11).Visible = False
                        lblProsStat4.Visible = False
                        txtProsStat4.Visible = False
                    Case 5
                        grdNational.Columns(12).Visible = False
                        lblProsStat5.Visible = False
                        txtProsStat5.Visible = False
                    Case 6
                        grdNational.Columns(13).Visible = False
                        lblProsStat6.Visible = False
                        txtProsStat6.Visible = False
                    Case 7
                        grdNational.Columns(14).Visible = False
                        lblProsStat7.Visible = False
                        txtProsStat7.Visible = False
                    Case 8
                        grdNational.Columns(15).Visible = False
                        lblProsStat8.Visible = False
                        txtProsStat8.Visible = False
                End Select

            Next
        End If
        Cursor.Current = Cursors.Default
    End Sub 


Private Sub FillManagerGrid()
        Cursor.Current = Cursors.WaitCursor
        ShowLabels()
        mStatusCol = StatusCollection.GetStatusList(mPromo.Id)
        mStatusList = StatusListAll.GetList(mPromoID, False)
        mStatusListShort = StatusListAll.GetList(mPromoID, True)
        If cboQuestion.SelectedIndex > 0 Then
            Select Case mQuestions(cboQuestion.SelectedIndex).DataType
                Case 0
                    mManager = ManagerSummaryList.GetManagerListByQuestionAnswer(mManagerID, mPromoID, cboEndDate.Value, mListType, txtAnswer.Text, mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)
                Case 1
                    mManager = ManagerSummaryList.GetManagerListByQuestionAnswer(mManagerID, mPromoID, cboEndDate.Value, mListType, cboAnswer.SelectedValue, mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)

                Case 2
                    mManager = ManagerSummaryList.GetManagerListByQuestionAnswer(mManagerID, mPromoID, cboEndDate.Value, mListType, IIf(chkAnswer.Checked, "1", "0"), mQuestions(cboQuestion.SelectedIndex).DataType, mQuestions(cboQuestion.SelectedIndex).Id)

            End Select
        Else

            mManager = ManagerSummaryList.GetManagerList(mManagerID, mPromoID, cboEndDate.Value, mListType)
        End If


        grdProspect.DataSource = mManager


        Dim I As Int32
        For I = 0 To grdProspect.Columns.Count - 1
            Debug.Print(I & " " & grdProspect.Columns(I).Name)
        Next
        Dim tmp As String


        For I = 1 To mStatusList.Count - 1
            Select Case I
                Case 1
                    tmp = grdProspect.Columns(8).HeaderText
                    tmp = mStatusListShort(I).Value
                    lblProsStat1.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(8).HeaderText = mStatusListShort(I).Value
                Case 2
                    lblProsStat2.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(9).HeaderText = mStatusListShort(I).Value
                Case 3
                    lblProsStat3.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(10).HeaderText = mStatusListShort(I).Value
                Case 4
                    lblProsStat4.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(11).HeaderText = mStatusListShort(I).Value
                Case 5
                    lblProsStat5.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(12).HeaderText = mStatusListShort(I).Value
                Case 6
                    lblProsStat6.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(13).HeaderText = mStatusListShort(I).Value
                Case 7
                    lblProsStat7.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(14).HeaderText = mStatusListShort(I).Value
                Case 8
                    lblProsStat8.Text = mStatusList(I).Value & ":"
                    grdProspect.Columns(15).HeaderText = mStatusListShort(I).Value
            End Select
        Next
        If mStatusList.Count <= 8 Then
            For I = mStatusList.Count To 8
                Select Case I
                    Case 1
                    Case 2
                    Case 3
                    Case 4
                        grdProspect.Columns(11).Visible = False
                        lblProsStat4.Visible = False
                        txtProsStat4.Visible = False
                    Case 5
                        grdProspect.Columns(12).Visible = False
                        lblProsStat5.Visible = False
                        txtProsStat5.Visible = False
                    Case 6
                        grdProspect.Columns(13).Visible = False
                        lblProsStat6.Visible = False
                        txtProsStat6.Visible = False
                    Case 7
                        grdProspect.Columns(14).Visible = False
                        lblProsStat7.Visible = False
                        txtProsStat7.Visible = False
                    Case 8
                        grdProspect.Columns(15).Visible = False
                        lblProsStat8.Visible = False
                        txtProsStat8.Visible = False
                End Select

            Next
        End If

        
        txtProsActualDollars.Text = mManager.ActualDollars
        If mManager.Count > 0 Then grdProspect.Rows(0).Selected = True
        txtProsContactGoal.Text = mManager.ContactCount
        txtProsContactGoalAcheived.Text = mManager.ContactGoalText
        txtProsContact.Text = mManager.ContactCount '& " - " & mManager.ContactPercent
        txtProsCount.Text = mManager.TotalCount
        txtProsCount2.Text = mManager.TotalCount
        txtProsSoldCount.Text = mManager.SoldCount & " (" & mManager.SoldPercent & ")"
        txtProsPotentialDollars.Text = mManager.QuoteDollars
        txtProsProspectGoal.Text = mManager.ProspectGoalAcheived
        txtProsProspectGoalAcheived.Text = mManager.ProspectGoalText
        txtProsSalesGoal.Text = mManager.SalesGoalColumnText
        txtProsSalesGoalAcheived.Text = mManager.SalesGoalText
        txtProsStat1.Text = mManager.Status1

        txtProsStat2.Text = mManager.Status2
        txtProsStat3.Text = mManager.Status3
        txtProsStat4.Text = mManager.Status4
        txtProsStat5.Text = mManager.Status5
        txtProsStat6.Text = mManager.Status6
        txtProsStat7.Text = mManager.Status7
        txtProsStat8.Text = mManager.Status8
        Cursor.Current = Cursors.Default
    End Sub



 Private Sub grdProspect_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdProspect.CellContentClick
        If Not mLoaded Then Exit Sub
        'If grdProspect.Columns.Count = 0 Then Exit Sub
        Cursor.Current = Cursors.WaitCursor
        If e.ColumnIndex = 0 And e.RowIndex > -1 Then
            Cursor.Current = Cursors.WaitCursor
            frmDashBoard.cboProsEmp.SelectedValue = grdProspect.Rows(e.RowIndex).Cells("EmployeeID").Value
            frmDashBoard.cboPromoID.SelectedValue = cboPromo.SelectedValue
            frmDashBoard.Show()
            Me.Hide()
            Cursor.Current = Cursors.Default
            Exit Sub
        End If

        Dim mStat As Status = Nothing
        Dim DoingSatusReport As Boolean = False
        Dim Emp As String
        Dim ZZ As String
        For I As Int32 = 0 To grdProspect.ColumnCount - 1
            ZZ = grdProspect.Columns(I).Name
        Next

        Select Case grdProspect.Columns(e.ColumnIndex).DataPropertyName
            Case "GridText1"
                mStat = mStatusCol.ItemBySequence(1)
                DoingSatusReport = True
            Case "GridText2"
                mStat = mStatusCol.ItemBySequence(2)
                DoingSatusReport = True
            Case "GridText3"
                mStat = mStatusCol.ItemBySequence(3)
                DoingSatusReport = True
            Case "GridText4"
                mStat = mStatusCol.ItemBySequence(4)
                DoingSatusReport = True
            Case "GridText5"
                mStat = mStatusCol.ItemBySequence(5)
                DoingSatusReport = True
            Case "GridText6"
                mStat = mStatusCol.ItemBySequence(6)
                DoingSatusReport = True
            Case "GridText7"
                mStat = mStatusCol.ItemBySequence(7)
                DoingSatusReport = True
            Case "GridText8"
                mStat = mStatusCol.ItemBySequence(8)
                DoingSatusReport = True

        End Select
        If DoingSatusReport And e.RowIndex >= 0 Then
            Emp = grdProspect.Rows(e.RowIndex).Cells("EmployeeID").Value
            Dim LPS As Int32
            LPS = grdProspect.Rows(e.RowIndex).Cells("EmployeeID").Value
            If radStatus.Checked Then

                Dim mProspectsByStatus As ProspectsByStatusList = Nothing

                mProspectsByStatus = FillProspectsbyStatus(Emp, 0, mStat)

                Dim rptPros As New rptProspectsByStatus
                rptPros.StatusText = mStat.Status
                rptPros.CSRM = mManagerID
                rptPros.StatusList = mProspectsByStatus
                rptPros.Run()
                Dim pdf As New DataDynamics.ActiveReports.Export.Pdf.PdfExport()
                Dim memStream As New System.IO.MemoryStream()
                pdf.Export(rptPros.Document, Application.StartupPath & "TempProspect.pdf")
                Process.Start(Application.StartupPath & "TempProspect.pdf")
            Else
                Dim mProsQA As ProspectQuestionsAnswersList
                mProsQA = FillQuestionsAnswers(Emp, 0, mStat.Id)
                Dim rptPros As New rptProspectQuestionAnswers

                rptPros.ProspectQuestionsAnswers = mProsQA

                rptPros.Run()

                Dim pdf As New DataDynamics.ActiveReports.Export.Pdf.PdfExport()
                Dim memStream As New System.IO.MemoryStream()

                pdf.Export(rptPros.Document, Application.StartupPath & "TempProspect.pdf")
                Process.Start(Application.StartupPath & "TempProspect.pdf")
            End If

        End If
        'grdProspect.Rows(0).Selected = True
        Cursor.Current = Cursors.Default
    End Sub



Private Sub grdNational_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdNational.CellContentClick
        If Not mLoaded Then Exit Sub
        Cursor.Current = Cursors.WaitCursor
        If e.ColumnIndex = 0 And e.RowIndex >= 0 Then
            mLoaded = False
            Cursor.Current = Cursors.WaitCursor
            mManagerID = grdNational.Rows(e.RowIndex).Cells("EmployeeID2").Value
            cboManager.SelectedValue = mManagerID
            cmdManagerNationalView.Text = "National View"
            FillManagerGrid()
            grdProspect.Show()
            grdNational.Hide()
            lblcboManager.Visible = True
            cboManager.Visible = True
            Cursor.Current = Cursors.Default
            Me.Text = "Manager View"
            mLoaded = True
        End If
        Dim mStat As Status = Nothing
        Dim DoingSatusReport As Boolean = False
        Dim Emp As String = 0

        If e.RowIndex >= 0 Then
            Emp = grdNational.Rows(e.RowIndex).Cells("EmployeeID2").Value

            Select Case grdNational.Columns(e.ColumnIndex).Tag
                Case "Status1"
                    mStat = mStatusCol.ItemBySequence(1)
                    DoingSatusReport = True
                Case "Status2"
                    mStat = mStatusCol.ItemBySequence(2)
                    DoingSatusReport = True
                Case "Status3"
                    mStat = mStatusCol.ItemBySequence(3)
                    DoingSatusReport = True
                Case "Status4"
                    mStat = mStatusCol.ItemBySequence(4)
                    DoingSatusReport = True
                Case "Status5"
                    mStat = mStatusCol.ItemBySequence(5)
                    DoingSatusReport = True
                Case "Status6"
                    mStat = mStatusCol.ItemBySequence(6)
                    DoingSatusReport = True
                Case "Status7"
                    mStat = mStatusCol.ItemBySequence(7)
                    DoingSatusReport = True
                Case "Status8"
                    mStat = mStatusCol.ItemBySequence(8)
                    DoingSatusReport = True

            End Select
        End If
        If DoingSatusReport And e.RowIndex >= 0 Then


            If radStatus.Checked Then



                Dim mProspectsByStatus As ProspectsByStatusList = FillProspectsbyStatus(0, Emp, mStat)


                Dim rptPros As New rptProspectsByStatus
                rptPros.StatusText = mStat.Status
                rptPros.CSRM = Emp
                rptPros.StatusList = mProspectsByStatus
                rptPros.Run()
                Dim pdf As New DataDynamics.ActiveReports.Export.Pdf.PdfExport()
                Dim memStream As New System.IO.MemoryStream()
                pdf.Export(rptPros.Document, Application.StartupPath & "TempProspect.pdf")
                Process.Start(Application.StartupPath & "TempProspect.pdf")
            Else
                Dim mProsQA As ProspectQuestionsAnswersList
                mProsQA = FillQuestionsAnswers(Emp, 0, mStat.Id)
                Dim rptPros As New rptProspectQuestionAnswers

                rptPros.ProspectQuestionsAnswers = mProsQA

                rptPros.Run()

                Dim pdf As New DataDynamics.ActiveReports.Export.Pdf.PdfExport()
                Dim memStream As New System.IO.MemoryStream()

                pdf.Export(rptPros.Document, Application.StartupPath & "TempProspect.pdf")
                Process.Start(Application.StartupPath & "TempProspect.pdf")
            End If
        End If
        Cursor.Current = Cursors.Default

    End Sub

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:rgn2121
Comment Utility
So everything is good now?
0
 
LVL 1

Author Comment

by:Misledman
Comment Utility
Yes and No.... The problem is fixed if I simply hide the form (Line 243). The form starts fine the first time I load. If I close the form instead of hide it, and then open it again I get an "index out of range" on line 151. The grid doesn't have any columns. Why would columns be gone..... It's weird. I do have it working by not closing the form (see line 243)
0
 
LVL 12

Expert Comment

by:rgn2121
Comment Utility
What are mNational and mManager?

Do you Call FillNationalGrid and FillManagerGrid on Form_Load?

What does mManager contain on line 135 after you have closed and reopened the form?
Many rows...how many columns?

Just trying to get a pull picture of the code flow...

0
 
LVL 1

Author Comment

by:Misledman
Comment Utility
On line 148, if I close the form and reopen it, I get an index out of range error. If I hide the form (in the FormClosing event I have to hide it. Not let it close) there or NO ISSUES whatsoever. I think it's a Visual Studio issue personally. Anyways, on form load I FillManagerGrid. The manager grid displays the Manager's employee data (Most managers have about 15 employees). I have a National view that shows totals for Managers (Different grid on same form). That view is shown by clicking the "National View" button (I hide the manager grid when I do this and show the National grid). There are 9 Managers. In National View, the Manager name is a link in the grid. If I click the Managers name, FillManagerGrid is run and the manager grid is shown (again) filled with the clicked manager's employee data. In the manager grid, each employee name is a link. If I click an employee link the whole form disappears and shows the employee details in a different form. I said "disappears" because I can't close this form. I can only hide it. If I close it, the next time I open it, I get an error at line 148..... Again, I think it's a Visual Studio issue....
0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 500 total points
Comment Utility
Again, I think it's a Visual Studio issue....
Not saying it isn't, but I pass data between forms all the time and don't have this issue.

Your mNational and mManager...what are they, lists, datatables?

When you debug, if you set a break point on line 135 and run the code and allow the form to close, what happens on line 135 when you get back to it after letting the form close.  You said it errors on line 148, well you set the datasource on 135 so I am curious what is in mManager at that point...number of columns, rows, etc....
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now