INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Datagrid to New Form Text boxes

Datagrid to New Form Text boxes

(OP)
So I have been trying to figure this issue out and am not sure where to begin.

I have a datagrid which I have coded where the user is able to select 1 row only. I wanted to create an edit button that would capture the data within that row and then populate another form's text boxes that the user would be able to edit. Then they would click another button that would update the table.

So basically I want the datagrid only to look at the data and then the text box form to edit and change data.

Any tips?

RE: Datagrid to New Form Text boxes

Well, there's quite a bit involved in something like this, so I'm going to give a broad overview and you can ask questions as needed.

First, create the edit form with the textboxes in it. Then, at the top of the form's code in the Declarations section put this code:

CODE

Dim drDataToEdit As DataRow

    Public Property DataToEdit() As DataRow
        Get
            Return drDataToEdit
        End Get
        Set(ByRef value As DataRow) '<- make sure this parameter is ByRef, not ByVal
            drDataToEdit = value
        End Set
    End Property


    Dim bCancel As Boolean

    Public Property CancelEdit() As Boolean
        Get
            Return bCancel
        End Get
        Set(ByVal value As Boolean)
            bCancel = value
        End Set
    End Property 

This is how you will pass the DataRow to be edited to the edit form.

Now in the form with the DataGrid, put this in the Declarations section:

Dim cm As CurrencyManager

Next, where you bind data to the DataGrid, put this code:

cm = DataGrid1.BindingContext(DataTable)

Note: use the name of your DataGrid, and the name of the DataTable bound to the grid.

Next, in the DataGrid's MouseUp event handler, put this code:

CODE

Private Sub DataGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
        If DataGrid1.CurrentRowIndex <> -1 Then
            DataGrid1.Select(dg.CurrentRowIndex)
        End If
    End Sub 
Again, use the name of your DataGrid.

Next, put an edit button and put this in the code of that button:

CODE

Dim dr As DataRow

    dr = cm.Current

    Dim fEdit as New frmEdit

    fEdit.DataToEdit = dr

    fEdit.ShowDialog()

    If fEdit.CancelEdit Then
        DataTable.RejectChanges()
    Else
        If DataTable.HasChanges() Then
            'code to save changes
        EndIf
    EndIf 

Now put 2 more buttons on the edit form. Name them "btnSave" and "btnCancel"

In the code for btnSave put this:

Me.Cancel = False
Me.Close

in the code for btnCancel put this:

Me.Cancel = True
Me.Close

That's pretty much it.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Datagrid to New Form Text boxes

(OP)
Thank you, this is a good kick off point for me.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close