How can I pass value from DataGridView to another DataGridView on another form? - vb.net

How can I pass values from a DataGridView to another DataGridView on another form? Please I need this answer deadly.
I have a form with a DataGridView, I am using this as invoice so at the event of end_edit for the field of item name I make a search by the first letters of this item name and put the result in another DataGridView on another form. I need to when I choose the item that I need from the form number 2(search form) to move to the same cell in the form number 1(invoice form) ?????
Thank you

You have to be more specific about your Question
given this scenario
form1 with a datagridview and form2 with a datagridview
you open form2 from form1 you would go to form2 and put sub new in it with reference to the main form like
Private f1 As Form1
Public Sub New(ByRef f As Form)
InitializeComponent()
Me.f1 = f
End Sub
on Form1 show your Form
Dim f2 As New Form2(Me)
f2.Show()
then on form 2 you have access to form1
assuming both tables have the same Datasource` bound
you can go on the ClickCell event from your Datagridview on Form2
and put in something like this to be at the same position on both DGV's
Private Sub xDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles xDataGridView.CellClick
f1.xBindingSource.Position = Me.yBindingSource.Position
End Sub
this however will only work if you don't allow sorting of the columns otherwise it will be in defferent rows.
if you want to pass a value from dgv on form2 to dgv in form1
then you can do something like this
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button2.Click
f1.xDataGridView.CurrentRow.Cells("MyCell").Value = Me.xDataGridView.CurrentRow.Cells("MyCell").Value
End Sub

Related

Passing value from one form to another using events in vb.net

I'm trying to pass a value from one form, let's call it Form1 to another form, Form2. When i double click a row in a listview that is in Form2, the value should be passed to a combobox in Form1.
I could get one of my other forms to do this using an event called PropertyChanged, but I can't seem to get it to work on other forms. I don't know if it is the fact that you can only have 1 event in the entire project and not have another with the same name. I'm missing something, but i just don't know where.
This is the code i used in Form2:
Public Event PropertyChanged As Action(Of Object)
Private Sub ListView2_DoubleClick(sender As Object, e as EventArgs) Handles ListView2.DoubleClick
RaiseEvent PropertyChanged(ListView1.SelectedItems(0).SubItems(0).Text)
End Sub
And this is the code I used in Form1:
Dim WithEvents f2 As Form2
Private Sub PropertyChanged(obj As Object) Handles f2.PropertyChanged
cmb_form1.Text = obj
End Sub
There are several ways to implement this. This one is quick and dirty.
It consists of writing up a property in Form2 which is public. When the user makes a choice, it goes in the property. Then Form1 reads the property before it disposes Form2.
Here's a little code snippet to better illustrate what I mean:
Public Class Form1
' This form has a button which opens a Form2 instance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 As New Form2 ' instantiate Form2
form2.ShowDialog() ' the user chooses a value
MessageBox.Show(form2.Result) ' get it before it's out of scope!
End Sub
End Class
Public Class Form2
Public Property Result As String ' the value is stored in there, it can be any type
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Result = "Clicked!" ' store the value you want to share between forms
Me.Close()
End Sub
End Class
Have fun!

VB.Net Add value from datagridview to listbox in another form

I have a trouble on VB.Net desktop programming, I have a Form with a ListBox, Called Form1, this form have a button that will appear another form with DataGridView inside that new Form, we will call this as Participant. Then I have a scenario, when User click the Button, it will call the Participant Form, and when user click the datagridview row in the Participant Form, it will get the value from that row and add the value to the ListBox in Form1, I have done getting the value from the row, but I can't add the value to the ListBox. This is my code to help you understand what I have done.
This is the code that will call the Participant Form :
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim myForm As New Participant
If myForm Is Nothing Then
myForm = New Participant
End If
myForm.Show()
End Sub
And this is the code that will add the value from DataGridView in Participant Form to the ListBox in Form1
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 1
If e.ColumnIndex = yourColumnIndex Then
participant_Share = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Form1.ListBox1.Items.Add(participant_Share)
End If
End Sub
But this code cannot insert the value to the ListBox, Please help me if you know how to solve this.
Thank you
You can change Participant constructor to accept a ListBox as parameter and store it in a local variable.
Private yourListBoxReference As ListBox
Public Sub New(ByVal yourListBox As ListBox)
InitializeComponent()
yourListBoxReference = yourListBox
End Sub
So you can call this form as:
myForm = New Participant(yourListBox)
And use the local variable to add an element to your ListBox:
yourListBoxReference.Items.Add(participant_Share)

passing data from textbox in form1 to textbox in opened form2 in visual basic

i have form1 to enter movies details
in this form1 i have a textbox called NVideosGenres
through this textbox i can open the form2 with a space
the form2 contains 5 combobox to let the users choose the genres if there is more then one
when user choose the genres they will be applied to a textbox in the same form like this way
for example i choose from three combobox
action - war - western
so now i have a problem because i know that to pass value i should do this
Videos.NVideosGenres.text = me.FinalGenres.text
me.close()
when i click on the button the form2 will close but the data don't pass to NVideosGenres in the form1
any help??
There are many, many ways to do this. One of the easiest ways would be to create a property on Form2 that holds a reference to your main calling form. Then you can set the values of the textboxes (or whatever) directly from Form2. This is definitely not the best way to do it, but it certainly is quick and easy.
Private Sub btnShowForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowForm2.Click
'Create a new instance of Form2 (called frmDetail in this example).
Dim frm2 As New frmDetail()
'Set the MyParentForm property of Form2 to this form.
frm2.MyParentForm = Me
'Show the form.
frm2.ShowDialog()
End Sub
Now in Form2, when you click the "OK" button to close the form, you can use the property that references your parent form to set the values directly.
' Property to hold a reference to the calling form.
Private mParentForm As frmMain
Public Property MyParentForm() As frmMain
Get
Return mParentForm
End Get
Set(ByVal value As frmMain)
mParentForm = value
End Set
End Property
' When I click the OK button, I will store the value of my various textboxes to properties.
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
'Set the value of the Genre textbox on the parent form to the value
'of the textbox on my current form.
MyParentForm.txtGenre.Text = txtGenre.Text
Me.Hide()
End Sub

Label won't update from separate form

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.
There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.
What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.
Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.
Code:
Form1 (AECSurveyForm)
Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMeterData()
End Sub
Public Sub LoadMeterData()
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
'utility
Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")
End Sub
Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click
If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then
AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)
AECMeteringDataForm.Show()
End If
End Sub
Form2 (AECMeteringDataForm)
Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString
AECMeteringDataAdapter.Update(AECMeteringData)
AECSurveyForm.LoadMeterData()
End Sub

How to pass text from one form to opened form at back?

I have two forms. One is InvoiceEntry and another is ListofSuppliers. There is a control "txtsnam" in InvoiceEntry.ListofSuppliers contains a Datagrid which has all the names of suppliers. When user presses f9 on this textbox ListofSupplier form is opened. The user select the name of supplier from grid and press enter. On this press enter i want to pass the name of supplier to the "txtsnam".I have used this code but its not working for me:
On InvoiceEntry:
Private Sub txtsnam_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtsnam.KeyDown`
If e.KeyCode = Keys.F9 Then
Dim lov As New SupplierLOV
lov.ShowDialog()
End If
End Sub
On ListofSuppliers:
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.CurrentRow.Cells(0).Value.ToString
End If
End Sub
I also tried with the help of property but its also not working. I placed the breakpoint on line 2 it executes bt text doesnot appear in txtsnam
Solution 1: Create an event in Form2 and subscribe to it in Form1 when you open the Form2. When pressing on the Return key, raise the event in Form2 and transmit the value as parameter. Form1 will receive your value.
Solution 2: Add a member to Form2 of type Form1 and pass Form1 as parameter to Form2 when you create Form2. In this way you have full access to Form1 properties and you can set it from code anytime. When you close Form2 you already have your data set in Form1.
Solution 3: Have a static provider class and store there your info that you want from different Forms. You can set and get these values from anywhere in your program.
Solution 4: ... there are many other ways. These were the simplest ones.
Try this
Private Sub RadGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadGridView1.KeyDown
If e.keycode=keys.Enter then
InvoiceEntry.txtsnam.text=RadGridView1.Currentcell.value
End If
End Sub