none
Sorting a DatagridView by two columns Date and the Time RRS feed

  • Question

  • Hi Good People

    Is it possible with a bound DataGridView to sort two columns.  Both Descending. with Timer 

    Is what I am trying to Achieve is, Say Every 3 minuets or so, Run A SORT on a DataGridView FOR TWO columns

    column 1 ( PickupDate ), Column 2 ( PickupTime )

    in the properties of my Binding Source, in Sort row I have typed PickupDate, PickupTime, the name of the columns I want to sort in my DataGridView. (Not knowing if the property setting will accept the two columns) But with this Property is correct how can I add descending part to this property.

    Or has the Above got to be done in code with inside a  event handle 

    Kind Regards 

    Gary


    Gary Simpson

    Friday, November 10, 2017 10:52 PM

Answers

  • Since you're using a BindingSource (which is a good idea), then consider using the .Sort property which is pretty powerful in what/how it works.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, November 10, 2017 11:21 PM ...typo
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:06 PM
    Friday, November 10, 2017 11:21 PM
  • Hi

    Here is some code that will sort ASC and/or DESC on two columns of a DataTable (your data source). NoTimers included, but the sort subs can easily be called form a Timer.

    ' Form1 with DataGridView1
    ' Button1 - Sort ASC
    ' Button2 - Sort DESC
    Public Class Form1
        Private myTable As New DataTable("MyTable")
        Dim r As New Random
        Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As EventArgs) Handles Me.Load
            With myTable
                .Columns.Add("Date1", GetType(System.DateTime))
                .Columns.Add("Date2", GetType(System.DateTime))
    
                For i As Integer = 0 To 19
                    .Rows.Add(Now.AddDays(r.Next(3, 11)), Now.AddDays(r.Next(3, 11)))
                Next
            End With
    
            With DataGridView1
                .DataSource = myTable
                .DefaultCellStyle.Font = New Font(.Font.FontFamily, 14)
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .DefaultCellStyle.Format = "ddd, dd MMM yyyy"
            End With
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
            SortASC()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SortDESC()
        End Sub
        Sub SortASC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName & " Asc, " & myTable.Columns(1).ColumnName & " Asc")
        End Sub
        Sub SortDESC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName & " Desc, " & myTable.Columns(1).ColumnName & " Desc")
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 11, 2017 1:40 AM
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:06 PM
    Friday, November 10, 2017 11:30 PM

  • Frank Do you know of any Examples I could view...

    Regards

    Gary


    Gary Simpson

    It's easy to get confused, but the XML that you're talking about is their data; don't let that stump you.

    Look again at this please:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.sort(v=vs.110).aspx

    If you'll look at the example shown at the bottom, the very last line is the one to notice:

    source1.Sort = "artist ASC, cd ASC"


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, November 11, 2017 1:19 AM

  • Hi Les 

    Thank again for getting back to me on another issue / Query,

    I have tried your subs but with my table name and columns Names I don't seem to be getting anywhere. It is Late So I will have another look tomorrow/today...

    Regards

    Gary


    Gary Simpson

    Hi

    The code Iposted gets the appropriate column name from the column index. You can just as easily use the column names themselves:

    EDIT: This example has been altered so that the Time column contains only Times within the same day of Date in Date column.

    The display is of Date part in first column and Time part in Time column. (NOTE: column names have been changed to reflect new editted code)

    ' Form1 with DataGridView1
    ' Button1 - Sort ASC
    ' Button2 - Sort DESC
    ' Label1 -sort type
    Public Class Form1
        Private myTable As New DataTable("MyTable")
        Dim r As New Random
        Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As EventArgs) Handles Me.Load
            With myTable
                .Columns.Add("Date", GetType(System.DateTime))
                .Columns.Add("Time", GetType(System.DateTime))
    
                For i As Integer = 0 To 59
                    Dim d As DateTime = Now.AddDays(r.Next(3, 11)).AddMinutes(r.Next(1, 59))
                    .Rows.Add(d.Date, d)
                Next
            End With
    
            With DataGridView1
                .DataSource = myTable
                .DefaultCellStyle.Font = New Font(.Font.FontFamily, 14)
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .Columns("Date").DefaultCellStyle.Format = " ddd, dd MMM yyyy "
                .Columns("Time").DefaultCellStyle.Format = " HH:mm "
            End With
            Label1.Text = "Unsorted"
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
            SortASC()
            Label1.Text = "Sorted ASC"
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SortDESC()
            Label1.Text = "Sorted DESC"
        End Sub
        Sub SortASC()
            myTable.DefaultView.Sort = ("Date Asc, Time Asc")
        End Sub
        Sub SortDESC()
            myTable.DefaultView.Sort = ("Date Desc, Time Desc")
        End Sub
    End Class



    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 11, 2017 3:12 PM code altered
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:07 PM
    Saturday, November 11, 2017 1:38 AM
  • This is ugly in regards to doing this with a TableAdapter/DataSet/BindingSource. You could use a setup as shown below where the timer is set to an interval of your choice.

    What it does, in PositionChanged event of the BindingSource is remember the current row primary key to a private variable. When the Timer Tick event fires unsubscribe from PositionChanged, re-populate the DataGridView then find the last row you were on and re-position. If the data has not been saved than either manually save it (not recommended w/o user consent) or lose changes.

    I setup the sort in the query FillByDoubleSort (of course you can do this in the BindingSource.Sort property).

    Would I use this? If my arm was twisted to use TableAdapters, guess so. I don't use TableAdapters. 

    Public Class Form1
        Private Sub OrdersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub OrdersBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.OrdersTableAdapter.FillByDoubleSort(Me.NorthWindAzureDataSet.Orders)
            Timer1.Enabled = True
        End Sub
        Private PrimaryKey As Integer = -1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            RemoveHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            OrdersTableAdapter.FillByDoubleSort(NorthWindAzureDataSet.Orders)
            If PrimaryKey > -1 Then
                ' OrderID is our primary key
                Dim position As Integer = OrdersBindingSource.Find("OrderID", PrimaryKey)
                If position > -1 Then
                    OrdersBindingSource.Position = position
                End If
            End If
            AddHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            Timer1.Enabled = True
        End Sub
        Private Sub OrdersBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles OrdersBindingSource.PositionChanged
            If OrdersBindingSource.Current IsNot Nothing Then
                ' OrderID is our primary key
                PrimaryKey = CType(OrdersBindingSource.Current, DataRowView).Row.Field(Of Integer)("OrderID")
            Else
                PrimaryKey = -1
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:07 PM
    Saturday, November 11, 2017 1:40 AM
    Moderator
  • Gary,

    FillByDoubleSort is a query I created in the .xsd file (the designer file for the data classes). In short the query (I have deleted it) looks something like SELECT DateOrdered,ShippedDate, etc FROM Customers ORDER BY DateOrdered,ShippedDate.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:08 PM
    Saturday, November 11, 2017 4:31 PM
    Moderator
  • Hi Gary,

    If you want to use BindingSource Sort property, you can do like this:

    If you want to run sort every three second, you can use Timer to do this:

      Dim timer As New Timer
        Dim dt As New DataTable
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Id", GetType(Integer))
            dt.Columns.Add("Column1", GetType(String))
            dt.Columns.Add("Column2", GetType(String))
            dt.Rows.Add(1, "white", "Cherry")
            dt.Rows.Add(2, "bule", "Mattew")
            dt.Rows.Add(3, "black", "Wenday")
            dt.Rows.Add(4, "green", "Nick")
            dt.Rows.Add(5, "gray", "Amy")
            dt.Rows.Add(6, "pinck", "Neal")
            BindingSource1.DataSource = dt
            'BindingSource1.Sort = "Column1, Column2 desc"
            DataGridView1.DataSource = BindingSource1
            timer.Interval = 5000
            timer.Enabled = True
            AddHandler timer.Tick, AddressOf timer_Tick
        End Sub
        Private Sub timer_Tick(sender As Object, e As EventArgs)
            BindingSource1.Sort = "Column1 desc"
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:08 PM
    Monday, November 13, 2017 8:58 AM
    Moderator

All replies

  • Since you're using a BindingSource (which is a good idea), then consider using the .Sort property which is pretty powerful in what/how it works.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, November 10, 2017 11:21 PM ...typo
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:06 PM
    Friday, November 10, 2017 11:21 PM
  • Hi

    Here is some code that will sort ASC and/or DESC on two columns of a DataTable (your data source). NoTimers included, but the sort subs can easily be called form a Timer.

    ' Form1 with DataGridView1
    ' Button1 - Sort ASC
    ' Button2 - Sort DESC
    Public Class Form1
        Private myTable As New DataTable("MyTable")
        Dim r As New Random
        Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As EventArgs) Handles Me.Load
            With myTable
                .Columns.Add("Date1", GetType(System.DateTime))
                .Columns.Add("Date2", GetType(System.DateTime))
    
                For i As Integer = 0 To 19
                    .Rows.Add(Now.AddDays(r.Next(3, 11)), Now.AddDays(r.Next(3, 11)))
                Next
            End With
    
            With DataGridView1
                .DataSource = myTable
                .DefaultCellStyle.Font = New Font(.Font.FontFamily, 14)
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .DefaultCellStyle.Format = "ddd, dd MMM yyyy"
            End With
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
            SortASC()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SortDESC()
        End Sub
        Sub SortASC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName & " Asc, " & myTable.Columns(1).ColumnName & " Asc")
        End Sub
        Sub SortDESC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName & " Desc, " & myTable.Columns(1).ColumnName & " Desc")
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 11, 2017 1:40 AM
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:06 PM
    Friday, November 10, 2017 11:30 PM
  • Since you're using a BindingSource (which is a good idea), then consider using the .Sort property which is pretty powerful in what/how it works.

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Hi Frank

    Thank you for getting back to me, I have read through some of the .Sort property from the link, And it is to do with Xml code. I have to admit, I know nothing about ( Xml ) apart from it helps run your program.. 

    Frank Do you know of any Examples I could view...

    Regards

    Gary


    Gary Simpson

    Saturday, November 11, 2017 1:10 AM
  • Hi

    Here is some code that will sort ASC and/or DESC on two columns of a DataTable (your data source). NoTimers included, but the sort subs can easily be called form a Timer.

    ' Form1 with DataGridView1
    ' Button1 - Sort ASC
    ' Button2 - Sort DESC
    Public Class Form1
        Private myTable As New DataTable("MyTable")
        Dim r As New Random
        Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As EventArgs) Handles Me.Load
            With myTable
                .Columns.Add("Date1", GetType(System.DateTime))
                .Columns.Add("Date2", GetType(System.DateTime))
    
                For i As Integer = 0 To 19
                    .Rows.Add(Now.AddDays(r.Next(3, 11)), Now.AddDays(r.Next(3, 11)))
                Next
            End With
    
            With DataGridView1
                .DataSource = myTable
                .DefaultCellStyle.Font = New Font(.Font.FontFamily, 14)
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .DefaultCellStyle.Format = "ddd, dd MMM yyyy"
            End With
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
            SortASC()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SortDESC()
        End Sub
        Sub SortASC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName + " Asc, " + myTable.Columns(1).ColumnName + " Asc")
        End Sub
        Sub SortDESC()
            myTable.DefaultView.Sort = (myTable.Columns(0).ColumnName + " Desc, " + myTable.Columns(1).ColumnName + " Desc")
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Hi Les 

    Thank again for getting back to me on another issue / Query,

    I have tried your subs but with my table name and columns Names I don't seem to be getting anywhere. It is Late So I will have another look tomorrow/today...

    Regards

    Gary


    Gary Simpson

    Saturday, November 11, 2017 1:15 AM

  • Frank Do you know of any Examples I could view...

    Regards

    Gary


    Gary Simpson

    It's easy to get confused, but the XML that you're talking about is their data; don't let that stump you.

    Look again at this please:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.sort(v=vs.110).aspx

    If you'll look at the example shown at the bottom, the very last line is the one to notice:

    source1.Sort = "artist ASC, cd ASC"


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, November 11, 2017 1:19 AM

  • Hi Les 

    Thank again for getting back to me on another issue / Query,

    I have tried your subs but with my table name and columns Names I don't seem to be getting anywhere. It is Late So I will have another look tomorrow/today...

    Regards

    Gary


    Gary Simpson

    Hi

    The code Iposted gets the appropriate column name from the column index. You can just as easily use the column names themselves:

    EDIT: This example has been altered so that the Time column contains only Times within the same day of Date in Date column.

    The display is of Date part in first column and Time part in Time column. (NOTE: column names have been changed to reflect new editted code)

    ' Form1 with DataGridView1
    ' Button1 - Sort ASC
    ' Button2 - Sort DESC
    ' Label1 -sort type
    Public Class Form1
        Private myTable As New DataTable("MyTable")
        Dim r As New Random
        Private Sub Form1_Load(ByVal sender As System.Object,
        ByVal e As EventArgs) Handles Me.Load
            With myTable
                .Columns.Add("Date", GetType(System.DateTime))
                .Columns.Add("Time", GetType(System.DateTime))
    
                For i As Integer = 0 To 59
                    Dim d As DateTime = Now.AddDays(r.Next(3, 11)).AddMinutes(r.Next(1, 59))
                    .Rows.Add(d.Date, d)
                Next
            End With
    
            With DataGridView1
                .DataSource = myTable
                .DefaultCellStyle.Font = New Font(.Font.FontFamily, 14)
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .Columns("Date").DefaultCellStyle.Format = " ddd, dd MMM yyyy "
                .Columns("Time").DefaultCellStyle.Format = " HH:mm "
            End With
            Label1.Text = "Unsorted"
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
            SortASC()
            Label1.Text = "Sorted ASC"
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SortDESC()
            Label1.Text = "Sorted DESC"
        End Sub
        Sub SortASC()
            myTable.DefaultView.Sort = ("Date Asc, Time Asc")
        End Sub
        Sub SortDESC()
            myTable.DefaultView.Sort = ("Date Desc, Time Desc")
        End Sub
    End Class



    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 11, 2017 3:12 PM code altered
    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:07 PM
    Saturday, November 11, 2017 1:38 AM
  • This is ugly in regards to doing this with a TableAdapter/DataSet/BindingSource. You could use a setup as shown below where the timer is set to an interval of your choice.

    What it does, in PositionChanged event of the BindingSource is remember the current row primary key to a private variable. When the Timer Tick event fires unsubscribe from PositionChanged, re-populate the DataGridView then find the last row you were on and re-position. If the data has not been saved than either manually save it (not recommended w/o user consent) or lose changes.

    I setup the sort in the query FillByDoubleSort (of course you can do this in the BindingSource.Sort property).

    Would I use this? If my arm was twisted to use TableAdapters, guess so. I don't use TableAdapters. 

    Public Class Form1
        Private Sub OrdersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub OrdersBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.OrdersTableAdapter.FillByDoubleSort(Me.NorthWindAzureDataSet.Orders)
            Timer1.Enabled = True
        End Sub
        Private PrimaryKey As Integer = -1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            RemoveHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            OrdersTableAdapter.FillByDoubleSort(NorthWindAzureDataSet.Orders)
            If PrimaryKey > -1 Then
                ' OrderID is our primary key
                Dim position As Integer = OrdersBindingSource.Find("OrderID", PrimaryKey)
                If position > -1 Then
                    OrdersBindingSource.Position = position
                End If
            End If
            AddHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            Timer1.Enabled = True
        End Sub
        Private Sub OrdersBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles OrdersBindingSource.PositionChanged
            If OrdersBindingSource.Current IsNot Nothing Then
                ' OrderID is our primary key
                PrimaryKey = CType(OrdersBindingSource.Current, DataRowView).Row.Field(Of Integer)("OrderID")
            Else
                PrimaryKey = -1
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:07 PM
    Saturday, November 11, 2017 1:40 AM
    Moderator
  • This is ugly in regards to doing this with a TableAdapter/DataSet/BindingSource. You could use a setup as shown below where the timer is set to an interval of your choice.

    What it does, in PositionChanged event of the BindingSource is remember the current row primary key to a private variable. When the Timer Tick event fires unsubscribe from PositionChanged, re-populate the DataGridView then find the last row you were on and re-position. If the data has not been saved than either manually save it (not recommended w/o user consent) or lose changes.

    I setup the sort in the query FillByDoubleSort (of course you can do this in the BindingSource.Sort property).

    Would I use this? If my arm was twisted to use TableAdapters, guess so. I don't use TableAdapters. 

    Public Class Form1
        Private Sub OrdersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub OrdersBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.OrdersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.NorthWindAzureDataSet)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.OrdersTableAdapter.FillByDoubleSort(Me.NorthWindAzureDataSet.Orders)
            Timer1.Enabled = True
        End Sub
        Private PrimaryKey As Integer = -1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            RemoveHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            OrdersTableAdapter.FillByDoubleSort(NorthWindAzureDataSet.Orders)
            If PrimaryKey > -1 Then
                ' OrderID is our primary key
                Dim position As Integer = OrdersBindingSource.Find("OrderID", PrimaryKey)
                If position > -1 Then
                    OrdersBindingSource.Position = position
                End If
            End If
            AddHandler OrdersBindingSource.PositionChanged, AddressOf OrdersBindingSource_PositionChanged
            Timer1.Enabled = True
        End Sub
        Private Sub OrdersBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles OrdersBindingSource.PositionChanged
            If OrdersBindingSource.Current IsNot Nothing Then
                ' OrderID is our primary key
                PrimaryKey = CType(OrdersBindingSource.Current, DataRowView).Row.Field(Of Integer)("OrderID")
            Else
                PrimaryKey = -1
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi Again Karen Payne,

    Thank you for getting back to me.

    I have tried your Code And Adapted My Data Table, Binding Source Etc. to My  Data Table, Binding Source names But I have an Error with the ( DoubleFill ) Part of the code. The Double Fill does not belong to my program...

    The Full Code I have is below ...

     Private Sub Bookings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'TaxiBookingsDataSet.Driver' table. You can move, or remove it, as needed.
            Me.DriverTableAdapter.Fill(Me.TaxiBookingsDataSet.Driver)
            'Me.DailyBookingsTableAdapter.Fill(Me.TaxiBookingsDataSet.DailyBookings)
            Me.DailyBookingsTableAdapter.FillByDoubleSort(Me.TaxiBookingsDataSet.DailyBookings)
            Timer2.Enabled = True
            LoadGrid()
            LoadJobsGrid()
            mtbTimeOfPickup.ValidatingType = GetType(Date)
            If txtDateOfPickup.Text = "" AndAlso txtPickupAddress.Text = "" Then
                cmdUpdate.Enabled = False
                cmdDeleteBooking.Enabled = False
            Else
                cmdUpdate.Enabled = True
                cmdDeleteBooking.Enabled = True
            End If
            cmdAllocateDriver.Enabled = False
        End Sub
    
        Private Sub DailyBookingsBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles DailyBookingsBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.DailyBookingsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.TaxiBookingsDataSet)
        End Sub
       
        Private PrimaryKey As Integer = -1
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            Timer1.Enabled = False
            RemoveHandler DailyBookingsBindingSource.PositionChanged, AddressOf DailyBookingsBindingSource_PositionChanged
            Me.DailyBookingsTableAdapter.FillByDoubleSort(Me.TaxiBookingsDataSet.DailyBookings)
            If PrimaryKey > -1 Then
                ' BookingID is our primary key
                Dim position As Integer = DailyBookingsBindingSource.Find("BookingID", PrimaryKey)
                If position > -1 Then
                    DailyBookingsBindingSource.Position = position
                End If
            End If
            AddHandler DailyBookingsBindingSource.PositionChanged, AddressOf DailyBookingsBindingSource_PositionChanged
            Timer1.Enabled = True
        End Sub
        Private Sub DailyBookingsBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles DailyBookingsBindingSource.PositionChanged
            If DailyBookingsBindingSource.Current IsNot Nothing Then
                ' BookingID is our primary key
                PrimaryKey = CType(DailyBookingsBindingSource.Current, DataRowView).Row.Field(Of Integer)("BookingID")
            Else
                PrimaryKey = -1
            End If
        End Sub

    Kind Regards 

    Gary


    Gary Simpson

    Saturday, November 11, 2017 3:59 PM
  • Gary,

    FillByDoubleSort is a query I created in the .xsd file (the designer file for the data classes). In short the query (I have deleted it) looks something like SELECT DateOrdered,ShippedDate, etc FROM Customers ORDER BY DateOrdered,ShippedDate.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:08 PM
    Saturday, November 11, 2017 4:31 PM
    Moderator
  • Gary,

    FillByDoubleSort is a query I created in the .xsd file (the designer file for the data classes). In short the query (I have deleted it) looks something like SELECT DateOrdered,ShippedDate, etc FROM Customers ORDER BY DateOrdered,ShippedDate.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi Karen 

    Do You mean something Like this....

     Private Sub FillByDoubleSort()
            SQL.AddParam("@PickupDate", dgvDailyBookings.SelectedRows.Item("PickupDate"))
            SQL.AddParam("@PickupTime", dgvDailyBookings.SelectedRows.Item("PickupTime"))
    
            SQL.ExecQuery("SELECT PickupDate, PickupTime " & _
                          "From DailyBookings " & _
                          "WHERE PickupDate=@PickupDate ORDER BY PickupDate, DESC" & _
                          "And PickupTime=@PickupTime ORDER BY PickupTime, DESC")
    
        End Sub

    Gary


    Gary Simpson

    Saturday, November 11, 2017 5:57 PM
  • Gary,

    FillByDoubleSort is a query I created in the .xsd file (the designer file for the data classes). In short the query (I have deleted it) looks something like SELECT DateOrdered,ShippedDate, etc FROM Customers ORDER BY DateOrdered,ShippedDate.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Or do you mean 


    Gary Simpson

    Saturday, November 11, 2017 6:15 PM
  • Yes Gary, that is what I mean.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, November 11, 2017 6:45 PM
    Moderator
  • Hi Gary,

    If you want to use BindingSource Sort property, you can do like this:

    If you want to run sort every three second, you can use Timer to do this:

      Dim timer As New Timer
        Dim dt As New DataTable
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Id", GetType(Integer))
            dt.Columns.Add("Column1", GetType(String))
            dt.Columns.Add("Column2", GetType(String))
            dt.Rows.Add(1, "white", "Cherry")
            dt.Rows.Add(2, "bule", "Mattew")
            dt.Rows.Add(3, "black", "Wenday")
            dt.Rows.Add(4, "green", "Nick")
            dt.Rows.Add(5, "gray", "Amy")
            dt.Rows.Add(6, "pinck", "Neal")
            BindingSource1.DataSource = dt
            'BindingSource1.Sort = "Column1, Column2 desc"
            DataGridView1.DataSource = BindingSource1
            timer.Interval = 5000
            timer.Enabled = True
            AddHandler timer.Tick, AddressOf timer_Tick
        End Sub
        Private Sub timer_Tick(sender As Object, e As EventArgs)
            BindingSource1.Sort = "Column1 desc"
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Gary Simpson Monday, November 13, 2017 5:08 PM
    Monday, November 13, 2017 8:58 AM
    Moderator
  • Hi Gary,

    If you want to use BindingSource Sort property, you can do like this:

    If you want to run sort every three second, you can use Timer to do this:

      Dim timer As New Timer
        Dim dt As New DataTable
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Id", GetType(Integer))
            dt.Columns.Add("Column1", GetType(String))
            dt.Columns.Add("Column2", GetType(String))
            dt.Rows.Add(1, "white", "Cherry")
            dt.Rows.Add(2, "bule", "Mattew")
            dt.Rows.Add(3, "black", "Wenday")
            dt.Rows.Add(4, "green", "Nick")
            dt.Rows.Add(5, "gray", "Amy")
            dt.Rows.Add(6, "pinck", "Neal")
            BindingSource1.DataSource = dt
            'BindingSource1.Sort = "Column1, Column2 desc"
            DataGridView1.DataSource = BindingSource1
            timer.Interval = 5000
            timer.Enabled = True
            AddHandler timer.Tick, AddressOf timer_Tick
        End Sub
        Private Sub timer_Tick(sender As Object, e As EventArgs)
            BindingSource1.Sort = "Column1 desc"
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hi Cherry

    Thank you very much for writing your code and showing, How my Issue was Done in a round About way.

    The code I have Now and it works for me is below...

     Private Sub Bookings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'TaxiBookingsDataSet.DailyBookings' table. You can move, or remove it, as needed.
            Me.DailyBookingsTableAdapter.Fill(Me.TaxiBookingsDataSet.DailyBookings)
            'Me.DailyBookingsTableAdapter.FillBy(Me.TaxiBookingsDataSet.DailyBookings)
            'TODO: This line of code loads data into the 'TaxiBookingsDataSet.Driver' table. You can move, or remove it, as needed.
            Me.DriverTableAdapter.Fill(Me.TaxiBookingsDataSet.Driver)
            'TODO: This line of code loads data into the 'TaxiBookingsDataSet.Driver' table. You can move, or remove it, as needed.
    
            Dim dt As New DataTable
            dt.Columns.Add("BookingId", GetType(Integer))
            dt.Columns.Add("PickupDate", GetType(String))
            dt.Columns.Add("PickupTime", GetType(String))
            dt.Columns.Add("AccountType", GetType(String))
            dt.Columns.Add("CustomerName", GetType(String))
            dt.Columns.Add("PickupAddress", GetType(String))
            dt.Columns.Add("DropAddress", GetType(String))
            dt.Columns.Add("Fare", GetType(String))
            dt.Columns.Add("VehicleRequired", GetType(String))
            dt.Columns.Add("DriverCallsign", GetType(String))
            dt.Columns.Add("DriverName", GetType(String))
            dt.Columns.Add("Notes", GetType(String))
            dt.Columns.Add("TakenBy", GetType(String))
            dt.Columns.Add("DateTimeInserted", GetType(String))
    
            If SQL.RecordCount > 0 Then
                dgvDriver.DataSource = SQL.SQLDS.Tables(0)
                dgvDriver.Rows(0).Selected = True
    
    
                DailyBookingsBindingSource.DataSource = dt
                DailyBookingsBindingSource.Sort = "PickupDate, PickupTime ASC"
                dgvDailyJobs.DataSource = DailyBookingsBindingSource
                Timer2.Interval = 5000
                Timer2.Enabled = True
                AddHandler Timer2.Tick, AddressOf Timer2_Tick
    
                DailyBookingsBindingSource.DataSource = dt
                DailyBookingsBindingSource.Sort = "PickupDate, PickupTime ASC"
                dgvDailyJobs.DataSource = DailyBookingsBindingSource
                Timer2.Interval = 5000
                Timer2.Enabled = True
                AddHandler Timer2.Tick, AddressOf Timer2_Tick
    
                'Load Grid = dgvDriver
                LoadDriverGrid()
                
    
                cmdSaveUpdate.Enabled = False
                cmdAddBooking.Focus()
                dtpDateOfPickup.Enabled = False
                mtbPickupTime.Enabled = False
                cmdTimeNow.Enabled = False
                txtFare.Enabled = False
                cbxAccountType.Enabled = False
                txtCustomerName.Enabled = False
                txtPickupAddress.Enabled = False
                txtDropAddress.Enabled = False
                cbxDriverCallsign.Enabled = False
                txtNotes.Enabled = False
                cmdSaveBooking.Enabled = False
                cmdTimeNow.Enabled = False
                cmdAllocateSave.Enabled = False
                cmdClearCancel.Enabled = False
                rb4Seats.Enabled = False
                rb6Seats.Enabled = False
                rb8Seats.Enabled = False
    
                txtPickupDate.BackColor = Color.White
                mtbPickupTime.BackColor = Color.White
                txtFare.BackColor = Color.White
                txtCustomerName.BackColor = Color.White
                txtPickupAddress.BackColor = Color.White
                txtDropAddress.BackColor = Color.White
                txtNotes.BackColor = Color.White
                txtAccountType.BackColor = Color.White
                txtBookingID.BackColor = Color.White
                txtDriverCallsign.BackColor = Color.White
                txtDriverName.BackColor = Color.White
                'Validate Masked textbox (mtbPickupTime)
                mtbPickupTime.ValidatingType = GetType(Date)
                'check if text is in textboxes (txtPickupDate) and (txtPickupAddress)
                If txtPickupDate.Text = "" AndAlso txtPickupAddress.Text = "" Then
                    cmdUpdate.Enabled = False
                    cmdDeleteBooking.Enabled = False
                Else
                    cmdUpdate.Enabled = True
                    cmdDeleteBooking.Enabled = True
                End If
                cmdAllocateDriver.Enabled = False
    
                If txtCustomerName.Text = "CASH" Then
                    txtAccountType.Text = "CASH"
                Else
                    txtAccountType.Text = "ACCOUNT"
                End If
            End If
           
        End Sub
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            DailyBookingsBindingSource.Sort = "PickupDate, PickupTime ASC"
    
        End Sub

    As you can see Cherry I have put this code in my form load Event..

    Best Regards and Thank you again

    Gary 

     

    Gary Simpson

    Monday, November 13, 2017 5:15 PM