Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refreshing a dynamically created DataGridView 1

Status
Not open for further replies.

combs

Programmer
Apr 18, 2002
78
US
Hi,

I'm having problems refreshing a dynamically created datagridview. It's not a problem to load the control and populate it with the correct information. When I add a record, it's inserted into the database (Access via oledb) correctly.

If I add the record, close the tabpage, move a record forward and then a record back on the controlling page, the record will then show up when I re-open the tabpage with the datagridview on it.... I just can't make it appear when I click on the save button (also dynamically created). I will attach screen shots of the pages to help explain...

I have re-filled the dataset that the datagridview is attached to after I add the record... still no luck...

When I go to refresh the datagridview I get an error indicating "Object reference not set to an instance of an object."

Please help!

(using VS2008 Pro)

Below is the code that creates the dynamic controls on the tab page where the problems are occuring....
Code:
Private Sub BuildEstimatedWorkControls()
        Dim ControlNameArray(30, 7) As String
[COLOR=green]
        'Type, Name, Text, X, Y, Bold, Visible, Enabled
        'TAX ID[/color]
        ControlNameArray(0, 0) = "Label"
        ControlNameArray(0, 1) = "lblTaxID"
        ControlNameArray(0, 2) = "Tax ID: "
        ControlNameArray(0, 3) = "20"
        ControlNameArray(0, 4) = "30"
        ControlNameArray(0, 5) = "False"
        ControlNameArray(0, 6) = "True"
        ControlNameArray(0, 7) = "True"
        ControlNameArray(1, 0) = "Textbox"
        ControlNameArray(1, 1) = "txtTaxID"
        ControlNameArray(1, 2) = m_TaxID
        ControlNameArray(1, 3) = "95"
        ControlNameArray(1, 4) = "30"
        ControlNameArray(1, 5) = "False"
        ControlNameArray(1, 6) = "True"
        ControlNameArray(1, 7) = "False"
        [COLOR=green]'ITEM[/color]
        ControlNameArray(2, 0) = "Label"
        ControlNameArray(2, 1) = "lblItem"
        ControlNameArray(2, 2) = "Item: "
        ControlNameArray(2, 3) = "20"
        ControlNameArray(2, 4) = "55"
        ControlNameArray(2, 5) = "False"
        ControlNameArray(2, 6) = "True"
        ControlNameArray(2, 7) = "True"
        ControlNameArray(3, 0) = "Combobox"
        ControlNameArray(3, 1) = "cmboItem"
        ControlNameArray(3, 2) = ""
        ControlNameArray(3, 3) = "95"
        ControlNameArray(3, 4) = "55"
        ControlNameArray(3, 5) = "False"
        ControlNameArray(3, 6) = "True"
        ControlNameArray(3, 7) = "True"
        [COLOR=green]'LENGTH[/color]
        ControlNameArray(4, 0) = "Label"
        ControlNameArray(4, 1) = "lblLength"
        ControlNameArray(4, 2) = "Length: "
        ControlNameArray(4, 3) = "20"
        ControlNameArray(4, 4) = "80"
        ControlNameArray(4, 5) = "False"
        ControlNameArray(4, 6) = "True"
        ControlNameArray(4, 7) = "True"
        ControlNameArray(5, 0) = "Textbox"
        ControlNameArray(5, 1) = "txtLength"
        ControlNameArray(5, 2) = ""
        ControlNameArray(5, 3) = "95"
        ControlNameArray(5, 4) = "80"
        ControlNameArray(5, 5) = "False"
        ControlNameArray(5, 6) = "True"
        ControlNameArray(5, 7) = "True"
        [COLOR=green]'WIDTH[/color]
        ControlNameArray(6, 0) = "Label"
        ControlNameArray(6, 1) = "lblWidth"
        ControlNameArray(6, 2) = "Width: "
        ControlNameArray(6, 3) = "20"
        ControlNameArray(6, 4) = "105"
        ControlNameArray(6, 5) = "False"
        ControlNameArray(6, 6) = "True"
        ControlNameArray(6, 7) = "True"
        ControlNameArray(7, 0) = "Textbox"
        ControlNameArray(7, 1) = "txtWidth"
        ControlNameArray(7, 2) = ""
        ControlNameArray(7, 3) = "95"
        ControlNameArray(7, 4) = "105"
        ControlNameArray(7, 5) = "False"
        ControlNameArray(7, 6) = "True"
        ControlNameArray(7, 7) = "True"
        [COLOR=green]'DEPTH[/color]
        ControlNameArray(8, 0) = "Label"
        ControlNameArray(8, 1) = "lblDepth"
        ControlNameArray(8, 2) = "Depth: "
        ControlNameArray(8, 3) = "20"
        ControlNameArray(8, 4) = "130"
        ControlNameArray(8, 5) = "False"
        ControlNameArray(8, 6) = "True"
        ControlNameArray(8, 7) = "True"
        ControlNameArray(9, 0) = "Textbox"
        ControlNameArray(9, 1) = "txtDepth"
        ControlNameArray(9, 2) = ""
        ControlNameArray(9, 3) = "95"
        ControlNameArray(9, 4) = "130"
        ControlNameArray(9, 5) = "False"
        ControlNameArray(9, 6) = "True"
        ControlNameArray(9, 7) = "True"
        [COLOR=green]'QUANTITY[/color]
        ControlNameArray(10, 0) = "Label"
        ControlNameArray(10, 1) = "lblQuantity"
        ControlNameArray(10, 2) = "Quantity: "
        ControlNameArray(10, 3) = "20"
        ControlNameArray(10, 4) = "155"
        ControlNameArray(10, 5) = "False"
        ControlNameArray(10, 6) = "True"
        ControlNameArray(10, 7) = "True"
        ControlNameArray(11, 0) = "Textbox"
        ControlNameArray(11, 1) = "txtQuantity"
        ControlNameArray(11, 2) = ""
        ControlNameArray(11, 3) = "95"
        ControlNameArray(11, 4) = "155"
        ControlNameArray(11, 5) = "False"
        ControlNameArray(11, 6) = "True"
        ControlNameArray(11, 7) = "True"
        [COLOR=green]'UNIT PRICE[/color]
        ControlNameArray(12, 0) = "Label"
        ControlNameArray(12, 1) = "lblUnitPrice"
        ControlNameArray(12, 2) = "Unit Price: "
        ControlNameArray(12, 3) = "20"
        ControlNameArray(12, 4) = "180"
        ControlNameArray(12, 5) = "False"
        ControlNameArray(12, 6) = "True"
        ControlNameArray(12, 7) = "True"
        ControlNameArray(13, 0) = "Textbox"
        ControlNameArray(13, 1) = "txtUnitPrice"
        ControlNameArray(13, 2) = ""
        ControlNameArray(13, 3) = "95"
        ControlNameArray(13, 4) = "180"
        ControlNameArray(13, 5) = "False"
        ControlNameArray(13, 6) = "True"
        ControlNameArray(13, 7) = "False"
        [COLOR=green]'TOTAL COST[/color]
        ControlNameArray(14, 0) = "Label"
        ControlNameArray(14, 1) = "lblTotalCost"
        ControlNameArray(14, 2) = "Total Cost: "
        ControlNameArray(14, 3) = "20"
        ControlNameArray(14, 4) = "205"
        ControlNameArray(14, 5) = "False"
        ControlNameArray(14, 6) = "True"
        ControlNameArray(14, 7) = "True"
        ControlNameArray(15, 0) = "Textbox"
        ControlNameArray(15, 1) = "txtTotalCost"
        ControlNameArray(15, 2) = ""
        ControlNameArray(15, 3) = "95"
        ControlNameArray(15, 4) = "205"
        ControlNameArray(15, 5) = "False"
        ControlNameArray(15, 6) = "True"
        ControlNameArray(15, 7) = "False"
        [COLOR=green]'INSPECTOR[/color]
        ControlNameArray(16, 0) = "Label"
        ControlNameArray(16, 1) = "lblInspector"
        ControlNameArray(16, 2) = "Inspector: "
        ControlNameArray(16, 3) = "20"
        ControlNameArray(16, 4) = "230"
        ControlNameArray(16, 5) = "False"
        ControlNameArray(16, 6) = "True"
        ControlNameArray(16, 7) = "True"
        ControlNameArray(17, 0) = "Combobox"
        ControlNameArray(17, 1) = "cmboInspector"
        ControlNameArray(17, 2) = ""
        ControlNameArray(17, 3) = "95"
        ControlNameArray(17, 4) = "230"
        ControlNameArray(17, 5) = "False"
        ControlNameArray(17, 6) = "True"
        ControlNameArray(17, 7) = "True"
        [COLOR=green]'FUND ASSOCIATED[/color]
        ControlNameArray(18, 0) = "Label"
        ControlNameArray(18, 1) = "lblFund"
        ControlNameArray(18, 2) = "Fund: "
        ControlNameArray(18, 3) = "20"
        ControlNameArray(18, 4) = "255"
        ControlNameArray(18, 5) = "False"
        ControlNameArray(18, 6) = "True"
        ControlNameArray(18, 7) = "True"
        ControlNameArray(19, 0) = "Combobox"
        ControlNameArray(19, 1) = "cmboFund"
        ControlNameArray(19, 2) = ""
        ControlNameArray(19, 3) = "95"
        ControlNameArray(19, 4) = "255"
        ControlNameArray(19, 5) = "False"
        ControlNameArray(19, 6) = "True"
        ControlNameArray(19, 7) = "True"
        [COLOR=green]'DATE CONTROL[/color]
        ControlNameArray(20, 0) = "Label"
        ControlNameArray(20, 1) = "lblDate"
        ControlNameArray(20, 2) = "Date: "
        ControlNameArray(20, 3) = "20"
        ControlNameArray(20, 4) = "280"
        ControlNameArray(20, 5) = "False"
        ControlNameArray(20, 6) = "True"
        ControlNameArray(20, 7) = "True"
        ControlNameArray(21, 0) = "DateTimePicker"
        ControlNameArray(21, 1) = "dteDate"
        ControlNameArray(21, 2) = ""
        ControlNameArray(21, 3) = "95"
        ControlNameArray(21, 4) = "280"
        ControlNameArray(21, 5) = "False"
        ControlNameArray(21, 6) = "True"
        ControlNameArray(21, 7) = "True"
        [COLOR=green]'CLOSE FORM BUTTON[/color]
        ControlNameArray(22, 0) = "Button"
        ControlNameArray(22, 1) = "btnCloseForm"
        ControlNameArray(22, 2) = "Close Form..."
        ControlNameArray(22, 3) = "35"
        ControlNameArray(22, 4) = "310"
        ControlNameArray(22, 5) = "False"
        ControlNameArray(22, 6) = "True"
        ControlNameArray(22, 7) = "True"
        [COLOR=green]'SAVE RECORD BUTTON[/color]
        ControlNameArray(23, 0) = "Button"
        ControlNameArray(23, 1) = "btnSaveRecord"
        ControlNameArray(23, 2) = "Save/Enter Record..."
        ControlNameArray(23, 3) = "150"
        ControlNameArray(23, 4) = "310"
        ControlNameArray(23, 5) = "False"
        ControlNameArray(23, 6) = "True"
        ControlNameArray(23, 7) = "False"
        [COLOR=green]'UNITS PROMPT[/color]
        ControlNameArray(24, 0) = "Label"
        ControlNameArray(24, 1) = "lblUnits"
        ControlNameArray(24, 2) = "This item measured in: "
        ControlNameArray(24, 3) = "200"
        ControlNameArray(24, 4) = "79"
        ControlNameArray(24, 5) = "False"
        ControlNameArray(24, 6) = "True"
        ControlNameArray(24, 7) = "True"
        [COLOR=green]'UNITS OF CURRENT ITEM[/color]
        ControlNameArray(25, 0) = "Label"
        ControlNameArray(25, 1) = "lblShowUnits"
        ControlNameArray(25, 2) = "SF"
        ControlNameArray(25, 3) = "310"
        ControlNameArray(25, 4) = "78"
        ControlNameArray(25, 5) = "True"
        ControlNameArray(25, 6) = "True"
        ControlNameArray(25, 7) = "True"
        [COLOR=green]'HELP LABEL[/color]
        ControlNameArray(26, 0) = "Label"
        ControlNameArray(26, 1) = "lblHelp"
        ControlNameArray(26, 2) = "Enter measurements in FEET"
        ControlNameArray(26, 3) = "200"
        ControlNameArray(26, 4) = "103"
        ControlNameArray(26, 5) = "False"
        ControlNameArray(26, 6) = "True"
        ControlNameArray(26, 7) = "True"
        [COLOR=green]'DATAGRIDVIEW[/color]
        ControlNameArray(27, 0) = "DataGridView"
        ControlNameArray(27, 1) = "EstimatedItems"
        ControlNameArray(27, 2) = ""
        ControlNameArray(27, 3) = "350"
        ControlNameArray(27, 4) = "30"
        ControlNameArray(27, 5) = "False"
        ControlNameArray(27, 6) = "True"
        ControlNameArray(27, 7) = "True"
        [COLOR=green]'CTEP CHECKBOX[/color]
        ControlNameArray(28, 0) = "Label"
        ControlNameArray(28, 1) = "lblCTEP"
        ControlNameArray(28, 2) = "CTEP: "
        ControlNameArray(28, 3) = "200"
        ControlNameArray(28, 4) = "255"
        ControlNameArray(28, 5) = "False"
        ControlNameArray(28, 6) = "True"
        ControlNameArray(28, 7) = "True"
        ControlNameArray(29, 0) = "CheckBox"
        ControlNameArray(29, 1) = "chkboxCTEP"
        ControlNameArray(29, 2) = ""
        ControlNameArray(29, 3) = "241"
        ControlNameArray(29, 4) = "254"
        ControlNameArray(29, 5) = "False"
        ControlNameArray(29, 6) = "True"
        ControlNameArray(29, 7) = "True"
        [COLOR=green]'CTEP PERCENTAGE COMBOBOX[/color]
        ControlNameArray(30, 0) = "Combobox"
        ControlNameArray(30, 1) = "cmboCTEP"
        ControlNameArray(30, 2) = ""
        ControlNameArray(30, 3) = "259"
        ControlNameArray(30, 4) = "255"
        ControlNameArray(30, 5) = "False"
        ControlNameArray(30, 6) = "False"
        ControlNameArray(30, 7) = "True"

        For i As Integer = 0 To UBound(ControlNameArray)
            If ControlNameArray(i, 0) <> vbNullString Then
                Select Case ControlNameArray(i, 0)
                    Case "Label"
                        Dim label As New Label
                        label.Name = ControlNameArray(i, 1)
                        label.Text = ControlNameArray(i, 2)
                        label.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        If ControlNameArray(i, 5) = "True" Then
                            label.Font = New System.Drawing.Font("MS Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point)
                        Else
                            label.Font = New System.Drawing.Font("MS Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Point)
                        End If
                        If ControlNameArray(i, 6) = "True" Then
                            label.Visible = True
                        Else
                            label.Visible = False
                        End If
                        If label.Name = "lblUnits" Then
                            label.Height = 18
                            label.Width = 112
                        ElseIf label.Name = "lblHelp" Then
                            label.Height = 18
                            label.Width = 150
                            label.ForeColor = Color.DimGray
                        ElseIf label.Name = "lblShowUnits" Then
                            label.ForeColor = Color.Red
                            label.Width = 30
                        ElseIf label.Name = "lblCTEP" Then
                            label.Height = 18
                            label.Width = 40
                        Else
                            label.Height = 18
                            label.Width = 75
                        End If
                        label.Enabled = ControlNameArray(i, 7)
                        label.TextAlign = ContentAlignment.MiddleLeft
                        label.Tag = i.ToString
                        Me.TabPage15.Controls.Add(label)
                    Case "Textbox"
                        Dim txtbox As New TextBox
                        txtbox.Name = ControlNameArray(i, 1)
                        txtbox.Text = ControlNameArray(i, 2)
                        txtbox.Refresh()
                        txtbox.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        txtbox.Enabled = ControlNameArray(i, 7)
                        txtbox.Height = 23
                        txtbox.Width = 100
                        txtbox.TextAlign = HorizontalAlignment.Left
                        txtbox.Tag = i.ToString
                        AddHandler txtbox.LostFocus, AddressOf TextBoxOnLeave
                        Me.TabPage15.Controls.Add(txtbox)
                    Case "Combobox"
                        Dim cmbobox As New ComboBox
                        cmbobox.Name = ControlNameArray(i, 1)
                        cmbobox.Text = ControlNameArray(i, 2)
                        cmbobox.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        cmbobox.Enabled = ControlNameArray(i, 7)
                        [COLOR=green]'height & width[/color]
                        cmbobox.Tag = i.ToString
                        If cmbobox.Name = "cmboItem" Then
                            cmbobox.Height = 23
                            cmbobox.Width = 210
                            cmbobox.DisplayMember = "ItemDescription"
                            cmbobox.ValueMember = "ItemID"
                            cmbobox.DropDownWidth = 250
                            cmbobox.DataSource = m_Dataset.Tables("tblWorkItems")
                            AddHandler cmbobox.SelectedIndexChanged, AddressOf WorkItemChanged
                            [COLOR=green]'AddHandler cmbobox.Leave, AddressOf WorkItemChanged[/color]
                        ElseIf cmbobox.Name = "cmboInspector" Then
                            cmbobox.Height = 23
                            cmbobox.Width = 100
                            cmbobox.DisplayMember = "Initials"
                            cmbobox.ValueMember = "Initials"
                            cmbobox.DataSource = m_Dataset.Tables("tblInspectors")
                            AddHandler cmbobox.SelectedIndexChanged, AddressOf InspectorChanged
                        ElseIf cmbobox.Name = "cmboFund" Then
                            cmbobox.Height = 23
                            cmbobox.Width = 100
                            cmbobox.DropDownWidth = 160
                            cmbobox.DisplayMember = "FundName"
                            cmbobox.ValueMember = "FundAbbrev"
                            cmbobox.DataSource = m_Dataset.Tables("tblFunds")
                        ElseIf cmbobox.Name = "cmboCTEP" Then
                            cmbobox.Height = 23
                            cmbobox.Width = 55
                            cmbobox.DropDownWidth = 60
                            cmbobox.DropDownHeight = 185
                            cmbobox.DisplayMember = "Percent"
                            cmbobox.ValueMember = "Percent"
                            cmbobox.DataSource = m_Dataset.Tables("tblCTEPPercent")
                            cmbobox.Visible = ControlNameArray(i, 6)
                        End If
                        Me.TabPage15.Controls.Add(cmbobox)

                    Case "DateTimePicker"
                        Dim dteDate As New DateTimePicker
                        dteDate.Name = ControlNameArray(i, 1)
                        dteDate.Text = ControlNameArray(i, 2)
                        dteDate.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        dteDate.Enabled = ControlNameArray(i, 7)
                        dteDate.Height = 23
                        dteDate.Width = 100
                        dteDate.Tag = i.ToString
                        dteDate.Format = DateTimePickerFormat.Short
                        AddHandler dteDate.ValueChanged, AddressOf DateTimePickerValueChanged
                        Me.TabPage15.Controls.Add(dteDate)
                    Case "Button"
                        Dim button As New Button
                        button.Name = ControlNameArray(i, 1)
                        button.Text = ControlNameArray(i, 2)
                        button.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        button.Enabled = ControlNameArray(i, 7)
                        button.Height = 23
                        button.Width = 100
                        button.Tag = i.ToString
                        AddHandler button.Click, AddressOf ButtonClick
                        Me.TabPage15.Controls.Add(button)
                    Case "CheckBox"
                        Dim chkbox As New CheckBox
                        chkbox.Name = ControlNameArray(i, 1)
                        chkbox.Text = ControlNameArray(i, 2)
                        chkbox.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        chkbox.Enabled = ControlNameArray(i, 7)
                        chkbox.Width = 15
                        chkbox.Checked = False
                        AddHandler chkbox.CheckedChanged, AddressOf CTEP_Checked
                        Me.TabPage15.Controls.Add(chkbox)
                    Case "DataGridView"
                        Dim dgv As New DataGridView
                        dgv.Name = ControlNameArray(i, 1)
                        dgv.Location = New Point(ControlNameArray(i, 3), ControlNameArray(i, 4))
                        dgv.Enabled = ControlNameArray(i, 7)
                        dgv.Height = 400
                        dgv.Width = 588
                        dgv.Tag = i.ToString
                        Me.TabPage15.Controls.Add(dgv)
                        AddHandler dgv.Paint, AddressOf dgv_paint
                        AddHandler dgv.CellValueChanged, AddressOf dgv_CellValueChanged
                        dgv.DataSource = m_Dataset.Tables("tblEstimatedWork")
                        dgv.Columns.Item("WorkID").Visible = False
                        dgv.Columns.Item("Depth").Visible = False
                        dgv.Columns.Item("Project").Visible = False
                        dgv.Columns.Item("OwnerID").Visible = False
                        dgv.Columns.Item("Description").Visible = False
                        dgv.Columns.Item("Item").ToolTipText = dgv.Columns.Item("Description").ToString
                        dgv.Columns("Price").DefaultCellStyle.Format = "c"
                        dgv.Columns("Cost").DefaultCellStyle.Format = "c"
                        dgv.Columns("Quantity").DefaultCellStyle.Format = "N2"
                        dgv.Columns.Item("Item").Width = 37
                        dgv.Columns.Item("Length").Width = 50
                        dgv.Columns.Item("Width").Width = 38
                        dgv.Columns.Item("Quantity").Width = 61
                        dgv.Columns.Item("Price").Width = 59
                        dgv.Columns.Item("Cost").Width = 68
                        dgv.Columns.Item("Unit").Width = 35
                        dgv.Columns.Item("Insp").Width = 34
                        dgv.Columns.Item("Entered").Width = 71
                        dgv.Columns.Item("Fund").Width = 35
                        dgv.Columns.Item("CTEP").Width = 37
                        dgv.Update()
                End Select
            End If
        Next

    End Sub
 
From what I can see, you are only using that local dgv variable to do the refresh. So my guess is that you haven't correctly set it to the control in your tab-page.

Set a break-point and see if TabPage15.Controls("dgv") evaluates to anything at all.
 
RiverGuy,

Thanks for your help. I am not familiar with setting break points and looking at the variable values.... I've spent some time putting break points in, but can't figure out how to access the values....

Won't the code below give me access to the datagridview control on the the tabpage?

Code:
.
.
.
Dim dgv As New DataGridView
dgv = CType(TabPage15.Controls("dgv"), DataGridView)
dgv.Refresh()
.
.
.

Thanks
 
No because you named your "EstimatedItems" not "dgv".

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Put a break-point on dgv.Refresh().

When it hits that break-point, highlight and select TabPage15.Controls("dgv") from the code on the line above. After it's highlighted, right-click and and choose "add to watch window." If the window lists its value as Nothing, then you haven't correctly found the control.
 
Sorwen,

Great catch on the name! That fixed it!

Now I just have to figure out how to stop re-listing any original items when it refreshes, but I think I remember doing that before....

RiverGuy, thanks for your valuable help as well. I really appreciate the fact that you people will take the time to help a guy out.

 
I guess I spoke to soon.... sort of....

When I put the first item into the datagridview (or an additional item if other items already exist) it does not show up....

Upon entering a second item, that one and the first one (that originally didn't show up) both appear. From that point on, everything works great. The items appear as entered.

When I close that tabpage and then enter it again for a new property, the process repeats.... It takes two entry's to get items to show up in the datagridview....

Any ideas what could cause this sort of behavior?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top