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.Appli cation.Ena bleVisualS tyles()
System.Windows.Forms.Appli cation. _
SetCompatibleTextRendering Default(Fa lse)
System.Windows.Forms.Appli cation.Run (New frmMain)
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.Appli
System.Windows.Forms.Appli
SetCompatibleTextRendering
System.Windows.Forms.Appli
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
So, now let me try your suggestion.
ASKER
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.VisualBa sic.Compil erServices .DesignerG enerated() > _
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("Mai nt").Cells (wbHomeWor kbook.Shee ts("Maint" ).Rows.Cou nt, 6).End(XlDirection.xlUp).R ow
'Define range for ClientNames for grid view.
With wbHomeWorkbook.Sheets("Mai nt")
blah, blah, blah
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.VisualBa
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("Mai
'Define range for ClientNames for grid view.
With wbHomeWorkbook.Sheets("Mai
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.
You need to call PopulateDGV on a button click etc within the form itself.
ASKER
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!
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!
ASKER
Let me accept your initial solution and start a second Q re refreshing the form.
ASKER
Simple, direct solution!
ASKER
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.VisualBa
Partial Class frmMain
Inherits System.Windows.Forms.Form
Public Sub New() 'FM 1.0 Initialize
MyBase.New()
InitializeComponent()
wbHomeWorkbook = xlApp.ActiveWorkbook
' LastRowNum = wbHomeWorkbook.Sheets("Mai
'Define range for ClientNames for grid view.
With wbHomeWorkbook.Sheets("Mai
LastRowNum = .Cells(.Rows.Count, 6).End(XlDirection.xlUp).R
rngClientNames = .Range(.Cells(3, 6), .Cells(LastRowNum, 16))
rngClientNames.Interior.Co
End With
'Populate DataGridView with ClientNames.
With Me.dgvOffices 'DataGridView
Dim loadArray(,) As Object
Dim k As Integer = 1 'Counter below
loadArray = wbHomeWorkbook.Sheets("Mai
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
Next
Next
End With
End Sub
'-------------------------
'Form overrides dispose to clean up the component list.
<System.Diagnostics.Debugg
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
blah, blah, blah...