hello, im sure ive asked this question already but cant find the thread. How do i transfer data from 1 form to another but in a datagrid view.

Example: I have data that ive entered in Form2. On Form 1 i have a datagridview box. Wen i press Send on Form2 i want certain data in textboxes etc to show up in certain columns on the datagridview. Without using a server or a database how do i do it? And then once ive done that how do i bring that form up with the data that ive previously wrote in the boxes to still show up, if i double click or click on a row to then open that form with the relevant data still there? If you know what i mean.

Cheers

Recommended Answers

All 10 Replies

Sean,

Before I even attempt to offer advice on this I want to make sure we have the goals well defined.

So here is how I have distilled your description.

Form1 contains: datagridview

Form2 contains: multiple textboxes and a "Send Button"

On clicking "Send", the contents of the textboxes are to be saved and used as the source data to define a row in the datagridview. You want the capability to repopulate the textboxes on Form2 by doubleclicking on a populated row of the datagridview on Form1. You did not ask for this, but I assume that you also want the ability to add new records and delete old ones?

Does this accurately describe what you want to accomplish? If not, please elaborate any needed changes.

On clicking "Send", the contents of the textboxes are to be saved and used as the source data to define a row in the datagridview. You want the capability to repopulate the textboxes on Form2 by doubleclicking on a populated row of the datagridview on Form1. You did not ask for this, but I assume that you also want the ability to add new records and delete old ones? Does this accurately describe what you want to accomplish? If not, please elaborate any needed changes.

Thats exactly what i want however i think ive got the add records bit sorted

I am attaching the VS2008 Solution, so you don't need to worry about copying all this stuff.

Ok,
Since you stated you did not want a database, I recommend using a user setting variable of type DataTable.

UserSettingDT2

With this you can do all the same stuff you can do if you where to retrieve the values from say a SQL Server database. It just eliminates that step and we make the built-in usersettings stuff do the saving and retrieval of the data.

We need to still define a data structure, but that is pretty easy. Substitute and add your textboxes for the 3 default ones I used. You can add this as a seperate class in VS or get paste it after the Form1 code.

Public Class StoredRecordsDT
    Inherits System.Data.DataTable
    Public Sub New()
        With Me.Columns
            .Add(New DataColumn("TextBox1", GetType(String)))
            .Add(New DataColumn("TextBox2", GetType(String)))
            .Add(New DataColumn("TextBox3", GetType(String)))
        End With
    End Sub
End Class

The code for Form1 (with the datagridview and bindingnavigator)". The bindingnavigator is just a toolstrip that keeps things looking consistant with other database applications,

'For info on location of the user.config file see: http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
Public Class Form1
   Private RecordsBindingSource As New BindingSource

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If My.Settings.Records Is Nothing Then
         'Table has not yet been stored, or has been set to Nothing
         My.Settings.Records = New StoredRecordsDT
         My.Settings.Records.TableName = "Records"
      End If

      'set bindingsource datasource to use the user settings datatable
      RecordsBindingSource.DataSource = My.Settings.Records

      'Change the default behavior of the AddNewItem
      'by default it adds a new item, but the add sequence is not
      'what we need, we will be handling this in the click event handler
      RecordsBindingNavigator.AddNewItem = Nothing
      'need to enabled it after setting AddNewItem=nothing
      BindingNavigatorAddNewItem.Enabled = True

      'Give the binding navigator something to work with
      RecordsBindingNavigator.BindingSource = RecordsBindingSource

      'Set the dgv to use the bindingsource for data
      dgvRecords.DataSource = RecordsBindingSource
      dgvRecords.AllowUserToAddRows = False
      dgvRecords.AllowUserToDeleteRows = False
      dgvRecords.ReadOnly = True

   End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'save the data before the application closes
        Me.Validate()
        RecordsBindingSource.EndEdit() 'finalize any remaining inputs
        My.Settings.Records.AcceptChanges()
        My.Settings.Save()
    End Sub

   Private Sub dgvRecords_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRecords.CellDoubleClick
      dgvRecords.Enabled = False
      If Form2.ShowDialog(RecordsBindingSource) = Windows.Forms.DialogResult.OK Then
         RecordsBindingSource.EndEdit()
      Else
         RecordsBindingSource.CancelEdit()
      End If
      dgvRecords.Enabled = True
   End Sub

   Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
      BindingNavigatorAddNewItem.Enabled = False
      RecordsBindingSource.AddNew()
      If Form2.ShowDialog(RecordsBindingSource) = Windows.Forms.DialogResult.Cancel Then
         'remove added record
         RecordsBindingSource.EndEdit()
         RecordsBindingSource.RemoveCurrent()
      Else
         RecordsBindingSource.EndEdit()
      End If
      BindingNavigatorAddNewItem.Enabled = True
   End Sub

End Class

The code for Form2 (with 3 textboxes). I have added a "Cancel" button to this one.

Public Class Form2
   Private ReturnValue As System.Windows.Forms.DialogResult
   Public Overloads Function ShowDialog(ByVal bs As BindingSource) As System.Windows.Forms.DialogResult
      TextBox1.DataBindings.Add("Text", bs, "TextBox1", False, DataSourceUpdateMode.OnPropertyChanged)
      TextBox2.DataBindings.Add("Text", bs, "TextBox2", False, DataSourceUpdateMode.OnPropertyChanged)
      TextBox3.DataBindings.Add("Text", bs, "TextBox3", False, DataSourceUpdateMode.OnPropertyChanged)
      ReturnValue = Windows.Forms.DialogResult.Cancel
      MyBase.ShowDialog()
      TextBox1.DataBindings.Clear()
      TextBox2.DataBindings.Clear()
      TextBox3.DataBindings.Clear()
      Return ReturnValue
   End Function

   Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
      ReturnValue = Windows.Forms.DialogResult.OK
      Me.Close()
   End Sub


   Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
      ReturnValue = Windows.Forms.DialogResult.Cancel
      Me.Close()
   End Sub

End Class

Do not be shy about asking questions, I tend to believe that others understand what I do in code.

Cheers for all the hard work there. Im using Visual basic 2010 express. Can i still use these codes or wont they work?

It is all still valid. You should be able to open the project I provided, it will just "upgrade" the solution file. Code is code.

Cheers. As you can see im no good on VB aha. Another question, those codes you kindly wrote for me, where do i put them, as in which element do i double click on to bring the code writer up etc, ive tried doing all this stuff before with something else and i didnt know where to put them etc cheers

Just extract the zip file to where ever you want the files. Then in VB2010 go to the menu File->>Open Project and navigate to where you stored the files. Just keep opening the folders until you are presented with the file "Example UserSetting DataTable.sln". Double-click on it.

Im really sorry, it won't let me open it as i dont have Visual Studio, im gonna try and get it but dont think its playing ball.

try somthing like this.

'on form1 we have two textboxes (txtName ,txtAge) and one button and on form2 we have grid having two columns ,
'name and age .
'now use this code at the button click event on the form1.
form2.show()
'now use this code at the load event of form2.

datagridview1.rows.add(form1.txtName.text,form2.txtAge.text)

'this will show textboxes data into a single row.
'now use this code at the double click event of your grid.
'i assume that form1 is already open.
form1.txtName.text = datagridview1.item(0,datagridview1.currentrow.index).value.tostring()
form1.txtAge.text = datagridview1.item(2,datagridview1.currentrow.index).value.tostring()

hope this will help you.

Regards

form1.txtName.text = datagridview1.item(0,datagridview1.currentrow.index).value.tostring()

cheers ill give that a go. just to clear up, textname and textage is textbox1.text or textbox2.text and form 1 would be reportingsystemfrontpage (Which is what i called it) and form2 will be occurancelog? just so i know when im adding the codes in, i know which parts to change to corresepond with my actual project thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.