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)
Rick WillefordAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Is the grid on frmMain?

If you have code in frmMain.Load event to load the grid, move that code to a sub and then just call that sub from Load event as well as wherever you want to refresh the grid.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rick WillefordAuthor Commented:
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...
0
Rick WillefordAuthor Commented:
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.
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Rick WillefordAuthor Commented:
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
0
CodeCruiserCommented:
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.
0
Rick WillefordAuthor Commented:
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!
0
Rick WillefordAuthor Commented:
Let me accept your initial solution and start a second Q re refreshing the form.
0
Rick WillefordAuthor Commented:
Simple, direct solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.