Access datatable in parentform from childform - vb.net

I have a parentform (let's call it frmParent) that contains a datagridview. The datagridview is populated from a datatable. The data in the datatable comes from an Excel file.
When a button is pressed, the childform (let's call it frmChild) is loaded in a panel.
Therefore I have the following code:
Private Sub OpenChildForm(childForm As Form, btnSender As Object)
If activeForm IsNot Nothing Then activeForm.Close()
ActivateButton(btnSender)
activeForm = childForm
childForm.TopLevel = False
childForm.FormBorderStyle = FormBorderStyle.None
childForm.Dock = DockStyle.Fill
Me.panelDesktopPanel.Controls.Add(childForm)
Me.panelDesktopPanel.Tag = childForm
childForm.BringToFront()
childForm.Show()
lblTitle.Text = UCase(childForm.Text)
End Sub
In the childform I have several text- and checkboxes. I would like to populate the text- and checkboxes with data from the datatable in the parent form.
I can't access the datatable from the childform with the following code:
Dim TableRow() As DataRow = frmParent.ds.Tables("TableName").Select("F1=5")
What I would like to know if it's possible to access the datatable in the parentform from the childform or do I have to "copy" the datatable from the parentform to the childform.
The data keeps the same, so the datatable stays the same.
Kind regards,
Björn

Related

changing image that on my selected child forms picturebox1

how change the image on picturebox1 in childform on vb.net 2010 my app is a mdiform and child, sub on module (for mdiform,property changing or getting codes are include mdiform) is gonna change the image property of the picturebox1 in the child form
The Me.MdiChildren property in the MdiContainer contains the references to all the children.
It is a 0-based indexed array. So the first child would be Dim f = Me.MdiChildren(0)
To create and add a new form, you can use this type of process in the MDI parent.
Private Sub AddNewForm()
Dim dlg As New ChildForm
dlg.WindowState = FormWindowState.Maximized
dlg.MdiParent = Me
dlg.Text = $"Child {Me.MdiChildren.Length}"
dlg.Show()
End Sub

How to reference a variable control on a variable form from another form? Need to get the button.Tag renamed in running program

Here's my problem:
I have a form with a treeview.
That treeview shows all:
other forms in my project as parents
all buttonnames as childs
all buttontags as childs of the buttonnames.
When i select a buttonname in the treeview i have the selection presented as
(textbox1 with the buttonname)
(textbox2 with the buttontag)
(textbox3 with the formname)
I have 1 empty textbox which i want to fill manually, to update the buttontag from the button selected in the treeview.
Either way, all code I have tried ain't updating anything.
This is the code so far, but doesn't seem to work...
My Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Hide()
Dim thisButtonName As String = TextBox3.Text ' This is the name of the button I'm looking for
Dim thisButtonName2 As String = TextBox2.Text ' this is the new tag name for that button
' Loop all controls in this form
For Each ctrl As Control In Controls
' Is this control a button
If TypeOf (ctrl) Is Button Then
' Is this the correct button
If CType(ctrl, Button).Name = thisButtonName Then
CType(ctrl, Button).Tag = thisButtonName2
End If
End If
Next
End If
Next
TreeView1.Nodes.Clear()
For Each formprop In My.Forms.GetType.GetProperties
Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
ControlsTree(node, form.Controls)
Next
End Sub
I think there are two basic problems here:
Your loop For Each ctrl As Control In Controls is not iterating over the controls in the other form object referenced by the frm variable. The Controls property is going to default to this form's set of Controls, not frm.Controls.
It seems like you are trying to change the Tag in the definition of that class at run-time. That is not possible, AFAIK, especially in what you're trying here. Each time you create a new instance of that form object, you are going to get that object initialized as how it was compiled. You can change the Tag values of a running instance of the object, but you can't change the default values of the class without using a technique like dependency injection.

Programatically setting CheckBox value in DataGridView on another Tab does not commit

I have a form with two tabs, each with a DataGridView.
DGV1 on Tab1 is bound to a DataTable on form load.
DGV2 on Tab2 is not bound on form load, but includes a CheckBoxColumn to be utilized later.
When I select a row in DGV1, DGV2 is populated and I set each checkbox on DGV2 to True.
However, after a cell in DGV1 is clicked and DGV2 is subsequently populated and the checkboxes are set to True, they are reverted to Nothing when I first go to Tab2. However, once I have "seen" DGV2 by clicking the second tab, I can go back to DGV1 and click the same row, and now the checkboxes will actually be checked.
I have tried using Refresh and RefreshEdit to no avail.
Public Class Form1
Private ConnectionString As String = "..."
Private Function GetDataTable(selectString) As DataTable
Dim adapter As New SqlDataAdapter(selectString, ConnectionString)
Dim dt As New DataTable
Dim bs As New BindingSource
adapter.Fill(dt)
bs.DataSource = dt
Return dt
End Function
Private Sub Form1_Load(...) Handles Me.Load
DGV1.DataSource = GetDataTable("SELECT * FROM ...")
End Sub
Private Sub DGV1_CellClick(...) Handles DGV1.CellClick
DGV2.DataSource = GetDataTable("SELECT * FROM ...")
For i = 0 To dt.Rows.Count - 1
DGV2.Item(0, i).value = True
Next
End Sub
End Class
This seems to only apply to the CheckBoxColumn, as I can programatically edit other non-checkbox cells in the same manner and the changes are committed correctly. Any suggestions?
The best solution I could come up with was to add a boolean column to the DataTable before setting it as the BindingSource, rather than adding the CheckBoxColumn to the DataGridView.
dt.Columns.Add("Selected", GetType(System.Boolean)).SetOrdinal(0)
Admittedly, I cannot figure out why this works as I am still programatically setting the values of the CheckBoxColumn of the DataGridView.

How to call MDIParent from windows form?

I have an MDI form called "MDIParent1", MDI child form "MDIChild1" and I have a windows form called "FrmTest".
Now there is a button called "btnTest" in "MDIChild1" form and here is the click event.
Dim V As New FrmTest
V.MdiParent = MDIParent1
V.Show()
But It couldn't load the "frmTest" form. Is there any another way to do so?
Thanks in advance.
Try This :
Dim V As New FrmTest
V.MdiParent = Me.MdiParent
V.Show()
The above assumes that MDIChild1.MdiParent is already set to MDIParent1
You could also do this :
Dim V As New FrmTest
V.MdiParent = Application.OpenForms("MDIParent1")
V.Show()
To close other forms, iterate through MdiChildren collection :
Dim MyMdiForm as Form = Application.OpenForms("MDIParent1")
For Each Frm As Form In MyMdiForm.MdiChildren
If Frm IsNot V Then
Frm.Close()
End If
Next

How to refresh a DataGridView when a dialog form is closed

I am trying to refresh a datagirdview when i add a new record using a dialog form. I would like to know how can i refresh my datagirdview. I have two Win Forms . Form A is called FrmContactDetailList which is having a datagridview which i is showing data from sql server. Below first block of code is used to bind data to the grid. which is given in form load event and also in this form i have a Button called "Add New Record" . Once i press this button its opening a win form which is opening another form. Below is that code which i used to open this in by button click event.
This will open Form B. Form is called FrmClientDetails. This form will have a text box and a save button . So once i enter the new name in the text box and press save i want the datagirdview which is in Form A to be updated . and show the new record once i close Form B. how can i achieve this.
This Code is used to bind the datagridview. I have given this is the form load event.
Sub GetContactList()
Dim BindData As New VoucherClass
Dim dt As DataTable = BindData .Get_Client_List
DataGridView.DataSource = dt
End Sub
Private Sub FrmContactDetailsList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetContactList()
End Sub
I have using this code to open the dialog form to enter the new data.
Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
Dim FrmNewContact As New FrmClientDetails
FrmNewContact.Owner = Me
FrmNewContact.ShowDialog()
End Sub
When the binding data change it is automatically reflected to the data grid view that is bond.
Edit:
Handle the FrmNewContact's Closing event. You can refresh your datagridview in that sub.
Dim WithEvents dialog As New FrmNewContact
Sub done() Handles dialog.FormClosed
Me.DataGridView1.Refresh()
End Sub
Next edit:
Dim BindData As New VoucherClass
Dim dt As DataTable = BindData .Get_Client_List
Declare them outside of the sub. So you should have this:
Dim BindData as VoucherClass
Dim dt as DataTable
Sub GetContactList()
BindData = New VoucherClass
dt = BindData .Get_Client_List
DataGridView.DataSource = dt
End Sub
Try this: Instead of FrmNewContact.ShowDialog() If FrmNewContact.ShowDialog() = DialogResult.OK Then Me.DataFridView.Refresh() End IF You may need to include a setter for your dialog result at the close action for your modal. Me.DialogResult = DialogResult.OK Me.Close()