-
Mar 30th, 2019, 04:54 AM
#1
Thread Starter
Lively Member
how to show permanently the new data in datagrv in other form
hi everyone
i want when i doubleclick in row in datagridv in form2 i want the click effect in datagridview that in form1
i do it but show only when i close the first form and reopened, i want form1 stay open and show the change permanently
Code:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim form As New Form1
form.str = DataGridView1.CurrentRow.Cells(0).Value.ToString
Me.Hide()
form.Show()
End Sub
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
If str = "" Then
'wait
Else
Dim cmd As New SqlCommand("select Numero,Codearticle from DetailReceptionFrs where Numero='" & str & "'", cn)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
End If
End Sub
-
Apr 2nd, 2019, 06:12 AM
#2
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
-
Apr 2nd, 2019, 07:55 AM
#3
Re: how to show permanently the new data in datagrv in other form
hi,
here a sample to pass a Value (from Textbox1) from on Form to another
create a new Project and add 2 Forms
put this in Form 1
just add a Button
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'create Instance Form2
Dim Frm As New Form2
'set the Property myValue to = houssem
Frm.myValue = "houssem"
'open Form modal
Frm.ShowDialog(Me)
'show/read myValue in Form2
MsgBox(Frm.myValue)
Frm.Dispose()
Frm = Nothing
End Sub
'for the DGV
'Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
' Dim row As DataGridViewRow = DataGridView1.CurrentRow
' TextBox1.Text = row.Cells(0).Value.ToString()
'End Sub
End Class
and this in Form2
add a Textbox
Code:
Public Class Form2
Public Property myValue() As String
'passed Value
Get
myValue = TextBox1.Text
End Get
Set(ByVal value As String)
TextBox1.Text = value
End Set
End Property
End Class
HTH
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 3rd, 2019, 02:45 AM
#4
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
thanks for help but i want open a form with datagridview and from there go to form2, in form2 i want show data in gridview from there when i click in row it show the first form and actualise the data without reopened it,
-
Apr 3rd, 2019, 08:16 AM
#5
Re: how to show permanently the new data in datagrv in other form
Sounds to me you want to simply use the same bindingsource for all your controls in your forms. You could pass the entire FormA Class to FormB thus providing access to any objects in FormA Class to FormB, or create a Sub Class in FormA with Public reference to objects in FormA class and pass subclass to FormB, which is my preferred method on passing things to from class to class
Code:
Public Class FormA
Dim BindSource As New BindingSource
'Setup your data/binding
Public Class StuffToPassFormB
Public BindSource As BindingSource = FormA.BindSource
End Class
Private Sub ButtonFormB_Click(sender As Object, e As EventArgs) Handles ButtonFormB.Click
Dim StuffToPassFormB As New StuffToPassFormB
Using FormB As New FormB(StuffToPassFormB)
FormB.ShowDialog()
End Using
End Sub
End Class
Code:
Public Class FormB
Dim BindSourceFromFormA As BindingSource
Public Sub New(FormAStuff As FormA.StuffToPassFormB)
InitializeComponent()
BindSourceFromFormA = FormAStuff.BindSource
'Bind your dgv to BindSourceFromFormA
End Sub
End Class
-
Apr 3rd, 2019, 09:09 AM
#6
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
the hall idea is like a file and sub file,when i click the parent file i open the child file,
i have in second form(form2) the parent datagridview(when i click here i use a column "name" to return to first form that i have a child datagridview that show up a data from that rows clicked)
i just do it but i dont want to close first form and reopen it that is simple, i want to let it open and have the simple click from the datagridview in form2(already seen in my code above)
-
Apr 3rd, 2019, 09:23 AM
#7
Re: how to show permanently the new data in datagrv in other form
I feel like I typed all that for no reason
-
Apr 3rd, 2019, 09:28 AM
#8
Re: how to show permanently the new data in datagrv in other form
I will take it a step further, if you cant figure it out from here then i dont know...
Code:
Public Class FormA
Dim BindSource As New BindingSource
'Setup your data/binding
Private Sub FormA_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = BindSource
End Sub
Public Class StuffToPassFormB
Public BindSource As BindingSource = FormA.BindSource
End Class
Private Sub ButtonFormB_Click(sender As Object, e As EventArgs) Handles ButtonFormB.Click
Dim StuffToPassFormB As New StuffToPassFormB
Using FormB As New FormB(StuffToPassFormB)
FormB.ShowDialog()
End Using
End Sub
End Class
Code:
Public Class FormB
Dim BindSourceFromFormA As BindingSource
Public Sub New(FormAStuff As FormA.StuffToPassFormB)
InitializeComponent()
BindSourceFromFormA = FormAStuff.BindSource
End Sub
Private Sub FormB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.DataBindings.Add("Text", BindSourceFromFormA, "ColumnName")
End Sub
End Class
-
Apr 3rd, 2019, 09:56 AM
#9
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
Originally Posted by kpmc
I will take it a step further, if you cant figure it out from here then i dont know...
Code:
Public Class FormA
Dim BindSource As New BindingSource
'Setup your data/binding
Private Sub FormA_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = BindSource
End Sub
Public Class StuffToPassFormB
Public BindSource As BindingSource = FormA.BindSource
End Class
Private Sub ButtonFormB_Click(sender As Object, e As EventArgs) Handles ButtonFormB.Click
Dim StuffToPassFormB As New StuffToPassFormB
Using FormB As New FormB(StuffToPassFormB)
FormB.ShowDialog()
End Using
End Sub
End Class
Code:
Public Class FormB
Dim BindSourceFromFormA As BindingSource
Public Sub New(FormAStuff As FormA.StuffToPassFormB)
InitializeComponent()
BindSourceFromFormA = FormAStuff.BindSource
End Sub
Private Sub FormB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.DataBindings.Add("Text", BindSourceFromFormA, "ColumnName")
End Sub
End Class
i am so grateful and apreciate that
and i am new in vb.net and i don't know what BindingSource and i never work with it and this TextBox1.DataBindings.Add("Text", BindSourceFromFormA, "ColumnName")
please any document that make it to me clear
-
Apr 3rd, 2019, 11:23 AM
#10
Re: how to show permanently the new data in datagrv in other form
Click the mouse on any object, property, method, etc in your source and hit the F1 key to bring up the documentation. By default it will be latest Framework version and C# examples, but you can select older frameworks (over on top left side) and switch to VB (see next to Edit icon on menu bar).
-
Apr 3rd, 2019, 02:35 PM
#11
Re: how to show permanently the new data in datagrv in other form
There is more than one way to do this. From your explanation it seems this is Parent/Child situation with two DataGridViews one on the Parent form and one on the Child form. I like the BindingSource method but if the Parent and Child are two different tables in the database then I think using two bindingsources with a DataRelation would work nicely.
Here is what I'm talking about,
Code:
Imports System.Data.SqlClient
Public Class ParentForm
Private con As New SqlConnection(My.Settings.BooksDBConnectionString)
Private ds As New DataSet
Private daParent As New SqlDataAdapter("Select Id, Author From Author", con)
Private daChild As New SqlDataAdapter("Select BookName, Author From Books", con)
Private dtParent As New DataTable("parent")
Private dtChild As New DataTable("child")
Private bsParent As New BindingSource
Private bsChild As New BindingSource
Private Sub ParentForm_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
LoadDataSet()
bsParent.DataSource = ds
bsParent.DataMember = "parent"
Me.DataGridView1.DataSource = bsParent
bsChild.DataSource = bsParent '
bsChild.DataMember = "ParentChild"
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub LoadDataSet()
ds.Tables.Add(dtParent)
ds.Tables.Add(dtChild)
daParent.Fill(ds.Tables("parent"))
daChild.Fill(ds.Tables("child"))
ds.Relations.Add("ParentChild", ds.Tables("parent").Columns("Author"), ds.Tables("child").Columns("Author"))
End Sub
Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick
If Me.DataGridView1.SelectedCells IsNot Nothing Then
Using frm As New ChildForm(bsChild)
frm.ShowDialog()
End Using
End If
End Sub
End Class
Code:
Imports System.Data.SqlClient
Public Class ChildForm
Private bs As BindingSource
Public Sub New(child As BindingSource)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
bs = child
End Sub
Private Sub ChildForm_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
Me.DataGridView1.DataSource = bs
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub ChildForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
bs.EndEdit()
End Sub
End Class
Last edited by wes4dbt; Apr 3rd, 2019 at 02:44 PM.
-
Apr 6th, 2019, 07:29 AM
#12
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
thank you for your help
i go forward with my project,the problem now is when i click in row in datagridview in form2 and return to form1 and displaye the data
when i want to return to form2 where i clicked and i want to change what i click in row didnt word the click i dont know why
usually when i click it return to 1st form but in return and reclick it didnt work thats the problem
-
Apr 6th, 2019, 12:59 PM
#13
Re: how to show permanently the new data in datagrv in other form
No way to know what's wrong, don't have any idea what your code looks like.
-
Apr 8th, 2019, 02:22 AM
#14
Thread Starter
Lively Member
Re: how to show permanently the new data in datagrv in other form
i used dialogResult to get my code work,now i want to understand binding source and dataset how it work because i have trouble to getting data from database and textfile in same time to one gridview
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|