none
Como hacer que este codigo lo pueda aplicar a 3 datagridview sin repetir el codigo RRS feed

  • Pregunta

  • Buenas como podría poner este código para que se pueda aplicar a 3 datagridview específicos sin repetir el código 

    este código lo que hace es mover filas del datagridview 

    Gracias

    'MUEVE FILAS DEL DATAGRIDVIEW
        Private dragBoxFromMouseDown As Rectangle
        Private rowIndexFromMouseDown As Integer
        Private rowIndexOfItemUnderMouseToDrop As Integer
        Private Sub datagridview1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
    
            ' The mouse locations are relative to the screen, so they must be
            ' converted to client coordinates.
            Dim clientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
    
            ' Get the row index of the item the mouse is below.
            rowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex
    
            If rowIndexOfItemUnderMouseToDrop = -1 Then Exit Sub
    
            If e.Effect = DragDropEffects.Move Then
                Dim rowToMove As DataGridViewRow
    
                rowToMove = TryCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
    
                DataGridView1.Rows.RemoveAt(rowIndexFromMouseDown)
                DataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove)
    
            End If
    
        End Sub
    
        Private Sub datagridview1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
            e.Effect = DragDropEffects.Move
    
        End Sub
    
        Private Sub datagridview1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove
    
            If e.Button = Windows.Forms.MouseButtons.Left Then
    
                If rowIndexFromMouseDown = -1 Then Exit Sub
    
                Try
                    ' If the mouse moves outside the rectangle, start the drag.
                    If dragBoxFromMouseDown <> Rectangle.Empty AndAlso Not dragBoxFromMouseDown.Contains(e.X, e.Y) Then
                        ' Proceed with the drag and drop, passing in the list item.                   
                        Dim dropEffect As DragDropEffects = sender.DoDragDrop(DataGridView1.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
                    End If
                Catch ex As Exception
                    ex.Data.Clear()
                End Try
    
            End If
    
        End Sub
    
        Private Sub datagridview1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    
            ' Get the index of the item the mouse is below.
            rowIndexFromMouseDown = sender.HitTest(e.X, e.Y).RowIndex
    
            If rowIndexFromMouseDown = -1 Then Exit Sub
            If rowIndexFromMouseDown <> -1 Then
    
                ' Remember the point where the mouse down occurred.
    
                ' The DragSize indicates the size that the mouse can move
                ' before a drag event should be started.               
                Dim dragSize As Size = SystemInformation.DragSize
    
                ' Create a rectangle using the DragSize, with the mouse position being
                ' at the center of the rectangle.
                dragBoxFromMouseDown = New Rectangle(New Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize)
    
            Else
    
    
                ' Reset the rectangle if the mouse is not over an item in the ListBox.
                dragBoxFromMouseDown = Rectangle.Empty
    
            End If
    
        End Sub
        'MUEVE FILAS DEL DATAGRIDVIEW

    lunes, 26 de noviembre de 2018 16:23

Respuestas

  • Gracias por responder greg_dorian

    En el codigo que as puesto aun aparece DataGridView1 <-- con esto no funcionara para todos los datagridview no?

    Gracias

    la idea es que consultes en hacer un UserControl Personalizado y lo llames del barra de herramientas del visual studio

    https://www.youtube.com/watch?v=mECkft9LG4k

    user-control-in-C-Sharp/

    • Marcado como respuesta Diablo_Rojo martes, 27 de noviembre de 2018 15:40
    lunes, 26 de noviembre de 2018 19:02

Todas las respuestas

  • hola diablo

    crea un UserControl

    class dgvPersonal : UserControl
    {
    private Rectangle dragBoxFromMouseDown;
    private int rowIndexFromMouseDown;
    private int rowIndexOfItemUnderMouseToDrop;
    private void datagridview1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    // The mouse locations are relative to the screen, so they must be
    // converted to client coordinates.
    Point clientPoint = DataGridView1.PointToClient(new Point(e.X, e.Y));
    // Get the row index of the item the mouse is below.
    rowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
    if (rowIndexOfItemUnderMouseToDrop == -1)
    return;
    if (e.Effect == DragDropEffects.Move)
    {
    DataGridViewRow rowToMove;
    rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
    DataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
    DataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
    }
    }
    private void datagridview1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
    {
    e.Effect = DragDropEffects.Move;
    }
    private void datagridview1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (e.Button == Windows.Forms.MouseButtons.Left)
    {
    if (rowIndexFromMouseDown == -1)
    return;
    try
    {
    // If the mouse moves outside the rectangle, start the drag.
    if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
    // Proceed with the drag and drop, passing in the list item.
    DragDropEffects dropEffect = sender.DoDragDrop(DataGridView1.Rows(rowIndexFromMouseDown), DragDropEffects.Move);
    }
    catch (Exception ex)
    {
    ex.Data.Clear();
    }
    }
    }
    private void datagridview1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    // Get the index of the item the mouse is below.
    rowIndexFromMouseDown = sender.HitTest(e.X, e.Y).RowIndex;
    if (rowIndexFromMouseDown == -1)
    return;
    if (rowIndexFromMouseDown != -1)
    {
    // Remember the point where the mouse down occurred.
    // The DragSize indicates the size that the mouse can move
    // before a drag event should be started.
    Size dragSize = SystemInformation.DragSize;
    // Create a rectangle using the DragSize, with the mouse position being
    // at the center of the rectangle.
    dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / (double)2), e.Y - (dragSize.Height / (double)2)), dragSize);
    }
    else
    // Reset the rectangle if the mouse is not over an item in the ListBox.
    dragBoxFromMouseDown = Rectangle.Empty;
    }
    }

    perdona por el codigo en c# pero ya es hora de mejorar tus habilidades de programación y usar un lenguaje tipado :D :D ;)

    lunes, 26 de noviembre de 2018 17:51
  • Gracias por responder greg_dorian

    En el codigo que as puesto aun aparece DataGridView1 <-- con esto no funcionara para todos los datagridview no?

    Gracias

    lunes, 26 de noviembre de 2018 18:55
  • Gracias por responder greg_dorian

    En el codigo que as puesto aun aparece DataGridView1 <-- con esto no funcionara para todos los datagridview no?

    Gracias

    la idea es que consultes en hacer un UserControl Personalizado y lo llames del barra de herramientas del visual studio

    https://www.youtube.com/watch?v=mECkft9LG4k

    user-control-in-C-Sharp/

    • Marcado como respuesta Diablo_Rojo martes, 27 de noviembre de 2018 15:40
    lunes, 26 de noviembre de 2018 19:02