Datagridview Drag and Drop

A few problems with my Datagridview Drag and Drop

Page 1 of 1

2 Replies - 15579 Views - Last Post: 26 May 2009 - 01:00 AM Rate Topic: -----

#1 jase81  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 02-March 09

Datagridview Drag and Drop

Posted 25 May 2009 - 04:14 PM

For Each DGR As DataGridViewRow In dgvJobs.SelectedRows
   
	' Added to stop error occuring when destination dgv columns do not exist
	If dgvProject.ColumnCount = 0 Then
		
		For Each ColumnHeader As DataGridViewColumn In dgvJobs.Columns
			Dim name As String = ColumnHeader.Name
			Dim header As String = ColumnHeader.HeaderText
			dgvProject.Columns.Add(name, header)
		Next

	End If
			   
	Dim i As Integer = DGR.Index
	Dim dr As DataGridViewRow = CType(dgvJobs.Rows(i).Clone, DataGridViewRow)
	dgvProject.Rows.Add(dr)

Next



Hi I'm making a form where I have two datagridviews. The first dgv is databound to a database. The second dgv is not databound. I want to allow users to drag rows from dgv1 and drop them on dgv2. At the moment I have managed to get it to the point where I can drag from dgv1 to dgv2 but the result is an empty row being added.

When I try adding a row without using .Clone I get this error "Row provided already belongs to a DataGridView control."

Any ideas, or better yet, solutions?

Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview Drag and Drop

#2 jase81  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 120
  • Joined: 02-March 09

Re: Datagridview Drag and Drop

Posted 25 May 2009 - 04:31 PM

Dim dr As DataGridViewRow = CType(dgvJobs.Rows(i).Clone, DataGridViewRow)

Dim c As Integer = 0
		 Do While c < dr.Cells.Count
				  dr.Cells(c).Value = DGR.Cells(c).Value
				  c += 1
Loop

dgvProject.Rows.Add(dr)




Ok I figured a way to make it work. I just had to populate the row data. If anyone has a better solution please post it. I'm only learning vb and .net so any help optimising my code would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Datagridview Drag and Drop

Posted 26 May 2009 - 01:00 AM

try this:
Private Sub DataGridView1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDoubleClick
		Dim obj As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
		If (obj.Count > 0) Then
			DataGridView2.Rows.Add(1)
			DataGridView2.Rows(0).Cells(0).Value = obj(0).Cells(0).Value
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1