Link to home
Start Free TrialLog in
Avatar of Rick Willeford
Rick Willeford

asked on

vb.net How to refresh Data in Form already open?

I am very new to vb.net and this group, so thanks in advance...

My Project starts in Sub Main and then I initialize/open the main form in frmMain.vb.
I need to refresh the datagridview in frmMain with new data in Sub Main.
If I just try to rerun the Sub below, I get an error message at the second line (re Rendering) because the form is already open.
I can't Close the frmMain from the Sub Main module in order to start over.
Suggestions? Tks!

    Sub ShowMainForm()  'From 3.1.0, 3.1.1
        Stop
        System.Windows.Forms.Application.EnableVisualStyles()
            System.Windows.Forms.Application. _
                 SetCompatibleTextRenderingDefault(False)
            System.Windows.Forms.Application.Run(New frmMain)
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
Avatar of Rick Willeford
Rick Willeford

ASKER

I like the simplicity of that.
Where would I move the code following InitializeComponent()? Original layout is below.
I created a new Sub just below Sub New() and moved all the range/grid code (everything following InitializeComponent)  and placed a Call to the new Sub Populate DGV in place of the code.

Strange thing happens in that I am limited to 7 rows in the grid. My data is dynamic and currently includes 9 rows. I get 'out of range' error if I try to stuff in all 9 rows. What in the world would have created that problem all of a sudden? Has been working before.
Thanks!

'Form Initialization  FM1.0
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmMain
    Inherits System.Windows.Forms.Form

    Public Sub New()    'FM 1.0  Initialize
        MyBase.New()
        InitializeComponent()
            wbHomeWorkbook = xlApp.ActiveWorkbook
            ' LastRowNum = wbHomeWorkbook.Sheets("Maint").Cells(wbHomeWorkbook.Sheets("Maint").Rows.Count, 6).End(XlDirection.xlUp).Row
            'Define range for ClientNames for grid view.
            With wbHomeWorkbook.Sheets("Maint")
                LastRowNum = .Cells(.Rows.Count, 6).End(XlDirection.xlUp).Row
                rngClientNames = .Range(.Cells(3, 6), .Cells(LastRowNum, 16))
                rngClientNames.Interior.ColorIndex = 34
            End With

            'Populate DataGridView with ClientNames.
            With Me.dgvOffices 'DataGridView
                Dim loadArray(,) As Object
                Dim k As Integer = 1    'Counter below
                loadArray = wbHomeWorkbook.Sheets("Maint").Range("F3:P9").value
                For i As Integer = 0 To 10 '6 to 16,  Col 0 is first in Array for actual Col. 6
                    For j As Integer = 0 To LastRowNum - 3    ' 3 To LastRowNum
                        If i = 0 Then   'First Col 6 needs new Row.
                            dgvOffices.Rows.Add(0 + k)
                            k = k + 1
                        End If
                        dgvOffices.Rows(j).Cells(i).Value = loadArray(j + 1, i + 1)
                    Next
                Next
            End With
    End Sub
'---------------------------------------------------------
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
blah, blah, blah...
I solved my own problem re number of rows. I had set a hard definition of Range elsewhere! Duh!!
So, now let me try your suggestion.
Well, now when I try to Call PopulateDGV() from Sub Main which is a Module, I get 'PopulateDGV is not declared...'.
That new sub is set up as Public Sub PopulateDGV() in the  Partial Class frmMain, as below. Some kind of Scope issue? Tks!
---------------------------------------------------
'Form Initialization  FM1.0
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmMain
    Inherits System.Windows.Forms.Form

    Public Sub New()    'FM 1.0  Initialize
        MyBase.New()
        InitializeComponent()
        PopulateDGV()
    End Sub
----------------------------------------------------------
    Public Sub PopulateDGV()
        wbHomeWorkbook = xlApp.ActiveWorkbook
        ' LastRowNum = wbHomeWorkbook.Sheets("Maint").Cells(wbHomeWorkbook.Sheets("Maint").Rows.Count, 6).End(XlDirection.xlUp).Row
        'Define range for ClientNames for grid view.
        With wbHomeWorkbook.Sheets("Maint")
blah, blah, blah
I am struggling to understand your project layout. sub main is usually used in C#. In VB, we usually have a main form and we set that as the startup form on project properties.

You need to call PopulateDGV on a button click etc within the form itself.
I am porting a VBA project into VB.Net, so between that and my lack of understanding, some of my structure is not the most efficient. Most code is in Module (not Class) Sub Main. Form initialization and events are in frmMain as a Class.
I got to the new Public Sub  from Sub Main that you suggested by calling frmMain.PopulateDVG(). That worked!
DVG data is updated, but now how do I 'refresh' the frmMain to show the 'updated' grid data?
Tks!
Let me accept your initial solution and start a second Q re refreshing the form.
Simple, direct solution!