Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-arranging rows in datagridView using mouse

Status
Not open for further replies.

CabinFever

Programmer
Sep 28, 2009
16
US
VB2008Express

I have a DataGridView bound to a database:
Me.mydata.Fill(Me.mydataDataSet4.myinfo)

What is the best method to move rows up/down using the mouse to manually re-sort.
Thanks in advance!

 

Take a look at this link, the posting by GuyJos near the bottom.


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!
 
A search for GuyJos is not coming up with any posts...
 
I have a feeling jebenson forgot to include the link in his post...

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post

 
DOH!!!

Here's the link.



Note to self: drink coffee before posting. [blush]

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!
 
a little confused (newbie)...looked at the link...is that a simple select row with mouse and move up or down then drop?
 
Still trying to re-arrange rows in my datagridview using mouse.
I have found this code online.
When ran it deletes the row that I select to move, but does not insert it in the new location.

Any advise?


Private Sub SkillGrid_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SkillGrid.DragDrop
Dim p As Point = SkillGrid.PointToClient(New Point(e.X, e.Y))
Dim newRow As DataGridViewRow = SkillGrid.Rows(SkillGrid.HitTest(p.X, p.Y).RowIndex)
'gets new row info
If newRow.Index >= 0 And newRow.Index < SkillGrid.Rows.Count Then
Dim oldRow As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
'gets dropped row info
If newRow.Index <> oldRow.Index Then
'if the two rows aren't the same it inserts dropped row
SkillGrid.Rows.RemoveAt(oldRow.Index)
SkillGrid.Rows.Insert(oldRow.Index, oldRow)
End If
End If
End Sub
Private Sub SkillGrid_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SkillGrid.DragEnter
e.Effect = DragDropEffects.Copy
End Sub

Private Sub SkillGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SkillGrid.MouseDown
If e.Button = MouseButtons.Left Then
Dim info As DataGridView.HitTestInfo = SkillGrid.HitTest(e.X, e.Y)
If info.RowIndex >= 0 Then
Dim view As DataGridViewRow = DirectCast(SkillGrid.Rows(info.RowIndex), DataGridViewRow)
If view IsNot Nothing Then
SkillGrid.DoDragDrop(view, DragDropEffects.Copy)
End If
End If
End If
End Sub

thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top