Dynamically Select Dynamically Created Datagridview

tommyready

Member
Joined
Jul 1, 2009
Messages
23
Programming Experience
3-5
I have an application that dynamically creates tabpages and datagridviews based on query results. After the tabs and datagridviews get created it also populates a combo box with all the tabs created. When the user select a tab from the combo box I want it to select the datagridview on that tab and then I have a third party app that convert it to excel. But Im getting an error about object not set to an instance because I cant figure out how to dynamically select the tab or datagridview. I'll paste some code.

This is how the tabs and datagridviews are created
VB.NET:
    Private Sub PopulateTabs()
        System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
        Try
            Dim tCount As Integer = NumberofTeams - 1
            Dim TeamNames(0 To tCount) As String

            TeamNames = FetchTeams()

            Dim RawDS As DataSet = FetchData("rawdata")
            dgRawData.DataSource = RawDS.Tables("TeamQuery")

            For i = LBound(TeamNames) To UBound(TeamNames)
                Dim TeamName As String = TeamNames(i)
                Dim dgv As New DataGridView

                DataArea.Controls.Add(New TabPage(TeamName))
                dgv.Height = dgRawData.Height
                dgv.Width = dgRawData.Width
                dgv.Location = New Point(0, 0)
                dgv.Name = "dg" + replace(TeamName," ","")
                cboTabSelect.Items.Add(TeamName)
                DataArea.Controls.Item(i + 1).Controls.Add(dgv)
                Dim ds As DataSet = FetchData(TeamName)
                dgv.DataSource = ds.Tables("TeamQuery")
                'MsgBox(i)
            Next
            btnExcel.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        System.Windows.Forms.Cursor.Current = Cursors.Default
    End Sub

Here is the convert to excel I modified to handle the string pasted from the combo box
VB.NET:
    Private Function ConvertToExcel(ByVal TeamName As String) As Boolean
        Try
            Dim DGName As String = "dg" + Replace(TeamName, " ", "")

            MsgBox(DGName)
            Dim Tab As New TabPage
            Tab = CType(DataArea.Controls(Trim(TeamName)), TabPage)
            Tab.Select()
            MsgBox(Tab.Name)
            Dim dg As DataGridView = DirectCast(Tab.Controls(DGName), DataGridView)

            Using saveFileDialog As SaveFileDialog = Me.GetExcelSaveFileDialog
                If (saveFileDialog.ShowDialog(Me) = DialogResult.OK) Then
                    Dim fileName As String = saveFileDialog.FileName
                    ExcelGenerator.Generate(dg).Save(fileName)
                    Process.Start(fileName)
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Function

Error: Object Reference not set to an object instance.
 
Back
Top