(NET) NET (2013)

DragAndDrop DataGridView Rows


In this case I mark only one column as draggable, definition of this column there is in line 11. Another column I use for other operation by handle of click.


   8:  Public Enum nColumn
   9:      FF = 0
  10:      Color = 1
  11:      DragAndDrop = 2
  12:      URL = 3
  13:      Login = 4
  14:      Comment = 5
  15:      Edit = 6
  16:      Del = 7
  17:  End Enum
  ...   
  28:  Public Class MainForm
  ...   
  33:      Public Shared Rows As List(Of Site)
  ...   
  35:      Public Shared RowType As List(Of String)
  36:   
  37:      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
  38:          DataGridView1.AutoGenerateColumns = False
  39:          Try
  40:              db1 = New Url_DB(ConnectionString)
  41:              Rows = db1.Sites.Where(Function(X) X.Type = 1).OrderBy(Function(X) X.Pos).ToList
  ...   
  51:      End Sub
  ...   
  ...   
 298:  #Region "DragAndDrop"
 299:      Dim RowIndexFromMouseDown As Integer
 300:      Dim ColumnIndexFromMouseDown As Integer
 301:      Dim DragRow As DataGridViewRow
 302:   
 303:      Private Sub dataGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles DataGridView1.MouseDown
 304:          RowIndexFromMouseDown = DataGridView1.HitTest(e.X, e.Y).RowIndex
 305:          ColumnIndexFromMouseDown = DataGridView1.HitTest(e.X, e.Y).ColumnIndex
 306:          If (RowIndexFromMouseDown <> -1 And ColumnIndexFromMouseDown = nColumn.DragAndDrop) Then
 307:              DataGridView1.Rows(RowIndexFromMouseDown).Selected = True
 308:              DragRow = DataGridView1.SelectedRows(0)
 309:              StatusToolStripLabel.Text = "DragAndDrop Start=" & RowIndexFromMouseDown
 310:              DataGridView1.DoDragDrop(DragRow, DragDropEffects.Move)
 311:          End If
 312:      End Sub
 313:   
 314:      Private Sub DataGridView1_DragEnter(sender As Object, e As DragEventArgs) Handles DataGridView1.DragEnter
 315:          If (DataGridView1.SelectedRows.Count > 0) Then
 316:              e.Effect = DragDropEffects.Move
 317:          End If
 318:      End Sub
 319:   
 320:      Private Sub DataGridView1_DragOver(sender As Object, e As DragEventArgs) Handles DataGridView1.DragOver
 321:          If e.Effect = DragDropEffects.Move Then
 322:              Dim ClientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
 323:              RowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(ClientPoint.X, ClientPoint.Y).RowIndex
 324:              StatusToolStripLabel.Text = "DragAndDrop Continue=" & RowIndexOfItemUnderMouseToDrop
 325:          End If
 326:      End Sub
 327:   
 328:      Private Sub DataGridView1_DragLeave(sender As Object, e As EventArgs) Handles DataGridView1.DragLeave
 329:          StatusToolStripLabel.Text = "DragLeave"
 330:      End Sub
 331:   
 332:      Dim RowIndexOfItemUnderMouseToDrop As Integer
 333:      Private Sub DataGridView1_DragDrop(sender As Object, e As DragEventArgs) Handles DataGridView1.DragDrop
 334:          If e.Effect = DragDropEffects.Move Then
 335:              Dim ClientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
 336:              RowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(ClientPoint.X, ClientPoint.Y).RowIndex
 337:              StatusToolStripLabel.Text = "DragAndDrop Finish, OldPosition=" & RowIndexFromMouseDown & ",NewPosition=" & RowIndexOfItemUnderMouseToDrop
 338:              ReorderRecordInDb(RowIndexFromMouseDown, RowIndexOfItemUnderMouseToDrop)
 339:          End If
 340:      End Sub
 341:   
 342:      Sub ReorderRecordInDb(FromPosition As Integer, ToPosition As Integer)
 343:          If FromPosition <> ToPosition Then
 344:              Dim FromRow As Site = CType(BindingSource1(FromPosition), Site)
 345:              Dim CurDBRecord As Site = Rows.Where(Function(X) X.I = FromRow.I).FirstOrDefault
 346:              Dim ToRow As Site = CType(BindingSource1(ToPosition), Site)
 347:              Dim OldPos As Integer = FromRow.Pos
 348:              Dim NewPos As Integer = ToRow.Pos
 349:              Dim RowsForReorder = db1.Sites.OrderBy(Of Integer)(Function(X) X.Pos).ToList
 350:              For I As Integer = 0 To RowsForReorder.Count - 1
 351:                  If RowsForReorder(I).Pos >= NewPos Then
 352:                      RowsForReorder(I).Pos += 1
 353:                  End If
 354:              Next
 355:              CurDBRecord.Pos = NewPos
 356:              db1.SaveChanges()
 357:              RowsForReorder = db1.Sites.OrderBy(Of Integer)(Function(X) X.Pos).ToList
 358:              For I As Integer = 0 To RowsForReorder.Count - 1
 359:                  RowsForReorder(I).Pos = I + 1
 360:              Next
 361:              db1.SaveChanges()
 362:              DataGridView1_RowFefresh()
 363:          End If
 364:      End Sub
 365:   
 366:  #End Region
  ...   


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/DragAndDropDataGridViewRows/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>