Results 1 to 16 of 16

Thread: Moving Contents of DataGridView Row to New Form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Moving Contents of DataGridView Row to New Form

    Hi Everyone,

    I'm working with a datagrid which I've been able to populate with my data to with no problems. What I'm trying to do now is doubleclick on a row of data within the datagrid, then pull the contents of that row to a new form, with fields for each cell of that row for editing. I decided that I'd try using e.rowindex in the double click event, but I'm only able to get the actual numeric index to appear in the form. How would you go about grabbing the actual contents of the selected row and moving them to a new form?

    Here is my current code. When executed the new form comes up, but its just displaying the number(index). I need to know how to extract the row contents so I can store them in a variable or something, so I can display in textboxes on the new form. Thanks in advance to anyone who can help me understand this.


    Code:
    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
            Dim holdcells As String
    
            If e.RowIndex >= 0 Then
                holdcells = e.RowIndex.ToString()
                EditTicket.Show()
                EditTicket.TextBox1.Text = holdcells
    
    
            End If
        End Sub
    End Class
    Last edited by WRCREX; Jul 24th, 2012 at 01:35 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    I've also tried the following code, but its just pulling the index with some text before it referencing the row (not the cell contents)

    Code:
     Private Sub DataGridView1_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
            ' Dim holdcells As String
            EditTicket.Show()
            EditTicket.TextBox1.Text = DataGridView1.CurrentRow.ToString()
    
           
    
    
    
        End Sub
    End Class

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Moving Contents of DataGridView Row to New Form


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    I sincerely appreciate you looking up that thread for me, but I'm having difficulty translating that thread to what I'm trying to do. I am specifically looking for how I could use the celldoubleclick event to pull a row inside the DGV to different fields on a textbox using the row and column coordinates. I then want to be able to edit the records, and hit "update" and have them update to the same row,column in the db. I am having the programmer's equivalent of writers block right now. I just cant conceptualize how to do this in the simplest way possible. Is there a way to take the text values of each cell in the row and place them in a public array, then just plug those items into the textbox on a new form, or is that way off base?
    Last edited by WRCREX; Jul 24th, 2012 at 02:48 PM.

  5. #5
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Moving Contents of DataGridView Row to New Form

    If i under stand correctly
    you are having a form which contains a datagridview control
    and you want to transfer the cell content of row toa new form
    and then edit the same in the new form
    affect the contents back the datagridview in the first form
    Is the above thoughts are corrects

    if so Some questions?
    why you need to open a new form and transfer the data to & from
    why it can not be in the same form,same grid-cell
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Moving Contents of DataGridView Row to New Form

    if you are quite required to deal between 02 or more forms
    try this link http://www.vbforums.com/showthread.php?t=682456

    and i sincerely say that try jmcillhenny signature + his blog regarding communication between the forms
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    Thanks guys. I will check these solutions out and report back to the thread shortly. The main reasons I need to have the grid row open in a new form is because there are some large fields that are better viewed in a multiline textbox, and there are fields that are hidden on the datagridview that will be used on the form

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Moving Contents of DataGridView Row to New Form

    The following copies the current row to another DataGridView. CloneColumns is needed if the target DataGridView does not have any columns. If the target DataGridView has columns but not the same columns as the source DataGridView then you need to make sure it does. So make sure the columns required exists via creating them thru the IDE or CloneColumns etc.

    Code:
    Private Sub DataGridView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TopGrid.MouseDoubleClick
       BottomGrid.CloneColumns(TopGrid)
       TopGrid.CurrentRow.CloneRowWithValues(BottomGrid)
    End Sub
    Place the following in a code module
    Suggested file name for this module DataGridViewExtensions.vb
    Code:
    Module DataGridViewExtensions
        ''' <summary>
        ''' Used to copy a row from one DataGridView to another DataGridView
        ''' </summary>
        ''' <param name="SingleRow"></param>
        ''' <param name="Target"></param>
       ''' <remarks>
       ''' Does not handle multiple rows selected
       ''' </remarks>
       <System.Diagnostics.DebuggerStepThrough()> _
       <System.Runtime.CompilerServices.Extension()> _
       Public Sub CloneRowWithValues(ByVal SingleRow As DataGridViewRow, ByVal Target As DataGridView)
    
          Dim Results As DataGridViewRow = CType(SingleRow.Clone(), DataGridViewRow)
    
          For Row As Int32 = 0 To SingleRow.Cells.Count - 1
             Results.Cells(Row).Value = SingleRow.Cells(Row).Value
          Next
    
          Target.Rows.Add(Results)
    
        End Sub
    
        ''' <summary>
       ''' Used to copy columns from another DataGridView to this DataGridView
       ''' </summary>
       ''' <param name="Self"></param>
       ''' <param name="CloneFrom"></param>
       ''' <remarks>
       ''' Only does cloning if Self has no columns
       ''' </remarks>
       <Runtime.CompilerServices.Extension()> _
       Public Sub CloneColumns(ByVal Self As DataGridView, ByVal CloneFrom As DataGridView)
    
          If Self.ColumnCount = 0 Then
             For Each c As DataGridViewColumn In CloneFrom.Columns
                Self.Columns.Add(CType(c.Clone, DataGridViewColumn))
             Next
          End If
    
       End Sub
    End Module

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    Okay, I've taken a look at the replies. Allow me to clarify further. I'm not trying to copy the contents of one datagridview to another. What I'm trying to do is double click a row in a gridview, and have all of the row's contents move to separate text boxes on a new form (one for each cell in the row), not a new gridview.

    The catch is that I need to be able to edit the items on the new form, hit update, and have the gridview on the first form reflect the updated info when refreshed (it needs to go right back into the SQL row where it came from). So I'm assuming that I will need some way to reference the row and column indexes when I pass the data to the new form so I will be able to update in SQL. I am just unaware of how to do this. Wouldnt clone just copy the contents and disregard where these contents need to be in the SQL database?

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Moving Contents of DataGridView Row to New Form

    Quote Originally Posted by WRCREX View Post
    Okay, I've taken a look at the replies. Allow me to clarify further. I'm not trying to copy the contents of one datagridview to another. What I'm trying to do is double click a row in a gridview, and have all of the row's contents move to separate text boxes on a new form (one for each cell in the row), not a new gridview.

    The catch is that I need to be able to edit the items on the new form, hit update, and have the gridview on the first form reflect the updated info when refreshed (it needs to go right back into the SQL row where it came from). So I'm assuming that I will need some way to reference the row and column indexes when I pass the data to the new form so I will be able to update in SQL. I am just unaware of how to do this. Wouldnt clone just copy the contents and disregard where these contents need to be in the SQL database?
    If your DataGridView is data bound to a DataTable and one column is the primary key you can get the column info from the datasource, in this case the DataTable, the primary key column can be used to update the data once you have hit the update button by locating the row using say DataTable.Select method. To get access to the current row it would be easier to use a BindingSource instead, in this case the DataTable is assigned to the Bindingsource data source which becomes the data source for the DataGridView. You then access the current row via BindingSource.Current, casted as a DataRowView. The BindingSource also has a Position property which allows you to position to the row (which should still be current at this time) or use BindingSource.Find to locate a row then use Position to position and then update the current row.

    Also to populate the TextBoxes why not Data Bind to the fields in the first forms DataGridView by way of casting the BindingSource datasource to a DataTable as per above.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    Thanks Kevin, I'm going to work with that advice. I'll report back when I get it working.

  12. #12
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Moving Contents of DataGridView Row to New Form

    vb Code:
    1. Dim Col1_Value As String
    2.         Dim Col2_Value As String
    3.         Dim AffectedRow_id As Integer = Me.DataGridView1.CurrentRow.Index
    4.  
    5.         '' Etc
    6.         '' now get the values into the variables
    7.         '' from the affected row
    8.         '' i assume that the affected row is your current row
    9.  
    10.         With Me.DataGridView1
    11.             Col1_Value = .Rows(AffectedRow_id).Cells(0).Value '' you can use the column name here
    12.             Col2_Value = .Rows(AffectedRow_id).Cells(0).Value
    13.         End With
    14.  
    15.         '' now you have got the row / cell values in the variables
    16.         '' next is you need to open the new form
    17.         '' and paste this vaiables & then esit it
    18.         '' and paste it back to the grid view
    19.  
    20.         '' now how to transfer the data to another form is defined in the
    21.         '' above thread http://www.vbforums.com/showthread.php?t=682456
    22.         '' REMEMBER :- you need the AffectedRow_id in the next form ,
    23.         '' trasfer that also along with other values
    24.  
    25.         '' now the next problem is how to transfer back the
    26.         '' edited values back to grid
    27.         '' from your text boxes form refer to the form on which gridview is available
    28.         '' while
    29.  
    30.         With formnameThatContains_the_datagridvie.DataGridView1
    31.             .Rows(AffectedRow_id).Cells(0).Value = Col1_Value '' edited at text boxes form
    32.             .Rows(AffectedRow_id).Cells(0).Value = Col1_Value '' edited at text boxes form
    33.         End With
    34.         Me.Close()
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    Kevin and Rain - you guys are amazing. I am getting to work on this right now and have already repped you both for the detailed help. I would love to know where you guys learned the core concepts regarding moving data (data adapters, data tables, row indexes, etc, etc)

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    78

    Re: Moving Contents of DataGridView Row to New Form

    Okay, I'm now able to do the following:

    1. I can doubleclick the datagridview row and have each cell display on a new form in individual text boxes.

    2. I can change the value of the first cell (in my case this is the customer name), and when I hit "update" on the new form (using the code below), I'm able to see the change in the datagrid.

    The problem is that the value that I am updating in the datagrid is not committing to the database. I've made sure that the grid is not set to read only. Here is the code for the "Update" button which is on the new form where I can edit each cell value:

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim Col1_Value As String
            Dim Col2_Value As String
            Dim col3_Value As String
            Dim Col4_Value As String
            Dim Col5_Value As String
            Dim col6_value As String
            Dim AffectedRow_id As Integer = activeTickets.DataGridView1.CurrentRow.Index
            Dim custid As String
    
    
            With activeTickets.DataGridView1
                Col1_Value = .Rows(AffectedRow_id).Cells(0).Value
                Col2_Value = .Rows(AffectedRow_id).Cells(2).Value
                col3_Value = .Rows(AffectedRow_id).Cells(3).Value
                Col4_Value = .Rows(AffectedRow_id).Cells(6).Value
                Col5_Value = .Rows(AffectedRow_id).Cells(5).Value
                col6_value = .Rows(AffectedRow_id).Cells(8).Value
            End With
    
    
            custid = TextBox1.Text
    
            With activeTickets.DataGridView1
                .Rows(AffectedRow_id).Cells(0).Value = custid
            End With
            
          
            Me.Close()
    
         
    
        End Sub
    End Class

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Moving Contents of DataGridView Row to New Form

    From looking at your code here is what you need to do.

    Before setting values back to the DataGridView using a connection object back to your database table and a command object write an SQL update statement that updates the data in the database using the primary key for the row to update the proper record in the database table. If the update executes correctly then assign data back to the DataGridView and if there is a failure then do not update the DataGridView but instead handle the exception.

    In regards to connection object if MS-Access OleDbConnection and command object would be a OleDbCommand.

    An example of an update can be found at the 6th post at the following link for VS2010 and a VS2005 version a few post down.
    http://social.msdn.microsoft.com/For...c-f437a8f9bdb5

  16. #16
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Moving Contents of DataGridView Row to New Form

    Quote Originally Posted by WRCREX View Post
    Kevin and Rain - you guys are amazing. I am getting to work on this right now and have already repped you both for the detailed help. I would love to know where you guys learned the core concepts regarding moving data (data adapters, data tables, row indexes, etc, etc)
    The information comes from working outside of projects learning how to do specific task rather then while coding a project. My perspective is as a developer never stop learning, take a portion of your day to learn. A really good method is to scan thru questions here and attempt to solve them. Always take time to read MSDN documentation on something you need to use but unclear how to use rather than simply making an attempt to work thru an issue. Doing this will make you a better developer.

    When you look at the heavy hitters here (the ones with much more knowledge then me) my bet is they started off and still do read MSDN documentation and know the various options open to them to provide quality information to the people asking questions here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width