Results 1 to 5 of 5

Thread: Datatable (DatatGridView) MoveLineUP - is my solution rubbish ?

  1. #1
    chris_asa is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2009
    Location
    Switzerland
    Posts
    24
    Reputation
    51

    Datatable (DatatGridView) MoveLineUP - is my solution rubbish ?

    I have the following code to move a line up in the ....datagridview (datatable, (database)):

    Code:
                If Not Me.dgvLines.CurrentRow Is Nothing Then
                    Dim i As Integer = Me.dgvLines.CurrentRow.Index
                    If i > 0 Then
                        With CurrentCase.Lines
                            Dim u As Integer = .Rows(i - 1).Item("LineSort")
                            Dim d As Integer = .Rows(i).Item("LineSort")
                            .Rows(i - 1).Item("LineSort") = d
                            .Rows(i).Item("LineSort") = u
                            .AcceptChanges()
                        End With
                        Me.dgvLines.Sort(Me.dgvLines.Columns("LineSort"), System.ComponentModel.ListSortDirection.Ascending)
                        IsDirty = True
                        Me.dgvLines.Rows(i - 1).Selected = True
                    End If
               End If

    CurrentCase object contains a disconnected datatable .Lines
    CurrentCase.Lines.Columns("LineSort") is there to persist the desired line-sequence at the db

    dgvLines is a datagridview <-- bindingsource <-- datatable (CurrentCase.Lines)

    IsDirty is a Private form Boolean that eventually triggers an update to the DB in code elsewhere.


    Is there a neater solution than u, d ?
    Is dgvLines.Sort the best way through this ?


    regards Chris

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,339
    Reputation
    1543
    That's basically the way I'd go as well except that I wouldn't call Sort grid but rather set the Sort of the BindingSource and just let it happen of its own accord thereafter. Also, is that AcceptChanges call really appropriate?

  3. #3
    chris_asa is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2009
    Location
    Switzerland
    Posts
    24
    Reputation
    51
    Thanks for the reply.

    BindingSource.Sort does indeed seem attractive (and, for now, I'll forgo the .AcceptChanges).

    However, I'm having MANY issues that I can't yet diagnose / explain / reliably repeat.
    It often seems to work correctly, but bizarre things happen if:
    - I select-moveup-moveup
    - I select-moveup, select-moveup, select-moveup, selectLastRow-moveup

    Bizarre might be:
    row stupidity: 1,2,3,4,5 with 5 selected after moveup becomes 1,2,5,4,3
    row madness: 1,2,3,4,5 with 5 selected after moveup becomes 1,2,5,4,3 and after another moveup 1,2,3,4,5 and another moveup 1,2,5,4,3 etc
    sometimes combined with
    my MultiSelect=False dgv ends up with a strange selection: one row highlit (no ">") plus another row (with ">") and the first .visible cell selected

    Where does the Bindingsource.Sort take place? The UI-Thread?


    Anyhow, it's brains are scrambled and I need to devote a few(?) hours to try to understand what is going on.
    Fun!

    regards Chris

    Here's the code that smells (no errors thrown):

    The moveup
    Code:
                If Not Me.dgvLines.CurrentRow Is Nothing Then
                    Dim i As Integer = Me.dgvLines.CurrentRow.Index
                    If i > 0 Then
                        With CurrentCase.Lines
                            Dim u As Integer = .Rows(i - 1).Item("LineSort")
                            Dim d As Integer = .Rows(i).Item("LineSort")
                            .Rows(i - 1).Item("LineSort") = d
                            .Rows(i).Item("LineSort") = u
    
                            'forget AcceptChanges for now
                            '.AcceptChanges()
    
                        End With
    
                        'forget dgv sort for now, and try bs.sort
                        'Me.dgvLines.Sort(Me.dgvLines.Columns("LineSort"), System.ComponentModel.ListSortDirection.Ascending)
    
                        IsDirty = True
                        'Me.dgvLines.Rows(i - 1).Selected = True
    
                    End If
                End If
    and the dgv init code:
    Code:
                With Me.dgvLines
    
                    '// remove any existing rows
                    '.Rows.Clear()
    
                    '// define binding source
                    Dim bs As BindingSource = New BindingSource
                    bs.DataSource = CurrentCase.Lines
                    bs.Sort = "LineSort ASC"
    
                    '// populate new rows
                    .DataSource = bs

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,339
    Reputation
    1543
    E.g.
    Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
    .Add("ID", GetType(Integer))
    .Add("Name", GetType(String))
    .Add("Ordinal", GetType(Integer))
    End With

    With table.Rows
    .Add(1, "Peter", 1)
    .Add(2, "Paul", 2)
    .Add(3, "Mary", 3)
    .Add(4, "John", 4)
    .Add(5, "Jenny", 5)
    .Add(6, "Linda", 6)
    End With

    With BindingSource1
    .DataSource = table
    .Sort = "Ordinal"
    End With

    DataGridView1.DataSource = BindingSource1
    End Sub

    Private Sub BindingSource1_CurrentChanged(sender As System.Object, e As System.EventArgs) Handles BindingSource1.CurrentChanged
    moveUpButton.Enabled = (BindingSource1.Position > 0)
    moveDownButton.Enabled = (BindingSource1.Position < BindingSource1.Count - 1)
    End Sub

    Private Sub moveUpButton_Click(sender As System.Object, e As System.EventArgs) Handles moveUpButton.Click
    Dim currentRow = DirectCast(BindingSource1.Current, DataRowView)
    Dim previousRow = DirectCast(BindingSource1(BindingSource1.Position - 1), DataRowView)
    Dim ordinal = currentRow("Ordinal")

    currentRow("Ordinal") = previousRow("Ordinal")
    previousRow("Ordinal") = ordinal

    BindingSource1.EndEdit()
    End Sub

    Private Sub moveDownButton_Click(sender As System.Object, e As System.EventArgs) Handles moveDownButton.Click
    Dim currentRow = DirectCast(BindingSource1.Current, DataRowView)
    Dim nextRow = DirectCast(BindingSource1(BindingSource1.Position + 1), DataRowView)
    Dim ordinal = currentRow("Ordinal")

    currentRow("Ordinal") = nextRow("Ordinal")
    nextRow("Ordinal") = ordinal

    BindingSource1.EndEdit()
    End Sub

    End Class

  5. #5
    chris_asa is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2009
    Location
    Switzerland
    Posts
    24
    Reputation
    51
    Huge thanks for the e.g.

    It is a copy/paste solution to my problem. More importantly, it tells me that I need to spend a couple of days getting to grips with all the good things BindingSource has to offer before I start pecking at the keyboard again.

    regards Chris

Bookmarks

Posting Permissions

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