How to order Datatable by row value? - vb.net

I have the below datatable, how can I sort the datatable by Today name, so for example if today is Monday, it should show at the end of the Datatable
I tried the following, but it is not working
Dim dv As DataView = dt3.DefaultView
dv.Sort = " DayLog desc"
Dim sortedDT = dv.ToTable()

Been some time since I've done some VB, surely you could do the following?
Dim data() AS DataRow = dt.Select()
Dim sortedData = data.OrderBy(Function(x) CInt([Enum].Parse(GetType(DayOfWeek), x.Item("DayLog"))) )
Dim sortedDataTable = dt.Clone()
For Each row In sortedData
sortedDataTable.ImportRow(row)
Next
Assuming the spelling of your days of the week is 100% correct
If you have the string value of the current day of the week or you know the int value, then you can adjust the sort using modulus
eg.
Dim todayStringVal = "Tuesday" // say you have this value from somewhere
Dim today = CInt([Enum].Parse(GetType(DayOfWeek), todayStringVal)) // convert to day of the week enum
Dim sortedData = data.OrderBy(Function(x) CInt(([Enum].Parse(GetType(DayOfWeek), x.Item("DayLog")) + 7 - today)) Mod 7) // Apply sorting
Finally if you want the above snippet to sort so that today's value is at the end of the list just change OrderBy to OrderByDescending
EDIT
To ensure today's value appears first in your list even though DayOfWeek's index starts at 0 you need to + 7 and subtract the index then mod by 7 to get the correct index to sort by
eg. Saturday's index on the DayOfWeek enum is 6 therefore 6 + 7 (Days of the week) = 13 then subtract by 6 = 7 finally 7 mod 7 is 0, so Saturday's new index becomes 0 and gets ordered first in the list

Related

Fill combobox with time values

I want fill my combobox with time values like (08:00, 08:10, 08:20 until 09:50) step=10 minutes but the result is like (8:00, 8:10, 8:20, 8:30, 9:-20,9:-10, 9:00, 9:10, 9:20).
My code doesn't show the value like 8:40, 8:50 and he also show negative value like 9:-20, 9:-10).
So please how can I resolve this problem?
Heure_rdv.Items.Clear()
Dim nbr_minute2 As String
For i = 480 To 590 Step 10
Dim nbr_heure As Integer = cint(i / 60)
Dim nbr_minute As Integer = (i - (nbr_heure * 60))
nbr_minute2 = CStr(nbr_minute) + "0"
If ((i - (nbr_heure * 60)) = 0) Then
Heure_rdv.Items.Add(CStr(nbr_heure) + ":" + nbr_minute2)
Else
Heure_rdv.Items.Add(CStr(nbr_heure) + ":" + CStr(nbr_minute))
End If
Next
This is how I should do try it in VBA.
Should be similar in VB.NET
DIM i As Integer 'Counter 1
Dim ii As Integer 'Counter 2
' make 1st loop for hours
for i = 8 To 9
' mkae 2nd loop for minutes
for ii = 0 To 50 Step 10
'
Heure_rdv.Items.Add(i & ":" & ii)
next
next
This could be done with fewer lines of code using linq
Dim steps = Enumerable.Range(0,6)
Dim items as List(Of String) = new List(Of String)()
items.AddRange(steps.Select(Function(x) "08:" & (x * 10).ToString("D2")))
items.AddRange(steps.Select(Function(x) "09:" & (x * 10).ToString("D2")))
Heure_rdv.DataSource = items
First we create a list of integers from 0 to 5 (6 elements), then using these elements we create the strings required multiplying each element of the integer list by ten and converting the result to a two digit string. We do this one time for the 8 hour and one time for the 9. Finally we could set the combo datasource to the resulting list of strings.

VB.NET : Populating Gridview with Dates (30 days interval)

I am trying to populate a gridview with dates.
For example the date today is 2/18/2019 and the next date within 30 days is 3/20/2019. so the desired output shoul'd look like this
No. Date
1 3/20/2019
2 4/19/2019
3 5/19/2019
etc.
but the result is
No. Date
1 3/20/2019
2 3/20/2019
3 3/20/2019
etc.
Here is my code so far.
Dim termCounter As Integer = 36
Dim today As DateTime = DateTime.Today
Dim dueDate As DateTime = today.AddDays(30)
Dim dtable As DataTable = New DataTable()
dtable.Columns.Add("No.")
dtable.Columns.Add("Payment Date")
Dim RowValues As Object() = {"", ""}
Dim dRow As DataRow
Dim tmpDate As Date
For i As Integer = 1 To termCounter
If GridAmortSched.RowCount <= 0 Then
RowValues(0) = i
RowValues(1) = dueDate.ToShortDateString
dRow = dtable.Rows.Add(RowValues)
Else
tmpDate = GridAmortSched.Rows(GridAmortSched.RowCount - 1).Cells(1).Value.ToString()
RowValues(0) = i
RowValues(1) = tmpDate.AddDays(30).ToShortDateString
dRow = dtable.Rows.Add(RowValues)
End If
Next
dtable.AcceptChanges()
GridAmortSched.DataSource = dtable
As is often the case, the code is behaving as you have told it to do, not at you want it to do :-).
Every time you go through the loop, you set tmpDate from the same source and then two lines later, you put the value into RowValues. :
tmpDate = GridAmortSched.Rows(GridAmortSched.RowCount - 1).Cells(1).Value.ToString()
You do not adjust your source by i, nor do you increment it. When you AddDays, you do not adjust the original value either (which would not work because of how and when you assign it).
Two easy fixes (two options)
One. The first is to index the days to be added against the loop value - the simple modification to the code below will achieve this. It is a quick fix and may be harder to maintain in the future - more to the point, is harder to spot if you change something such as the loop starting point.
RowValues(1) = tmpDate.AddDays(30*i).ToShortDateString
Two. The other option is to address the code logic. Set your baseline value and then increment it within the loop.
Dim tmpDate As Date
tmpDate = GridAmortSched.Rows(GridAmortSched.RowCount - 1).Cells(1).Value.ToString() '*** Moved outside of loop
For i As Integer = 1 To termCounter
If GridAmortSched.RowCount <= 0 Then
RowValues(0) = i
RowValues(1) = dueDate.ToShortDateString
dRow = dtable.Rows.Add(RowValues)
Else
RowValues(0) = i
tmpDate = tmpDate.AddDays(30) '*** Increment date every time it passes here
RowValues(1) = tmpDate.ToShortDateString '*** Assign the new value
dRow = dtable.Rows.Add(RowValues)
End If
Next
dtable.AcceptChanges()
GridAmortSched.DataSource = dtable

Adding Day to sepcial Date with Duplicating count of friday

Im trying to make Recursive Function in VB.net
The Function are going to add some day to Special date . also if there is a
friday between the range of added day its going to count them and adding them to
date again.
it must be Recursive because if there way a friday after adding more than 7 day
it also must add another day to date. like this :
origianl date : 5/19/2015
day to be added : 30
added date : 6/18/2015
count of friday between 5/19/2015 and 6/18/2015 : 4
new date after adding fridays : 6/22/2015
count of friday that happen after addinf old friday count : 1
new date and final result : 6/23/2015
i think the last two step must be recursive. this what ive done so far without last two step :
Public Function CountOfFriday(ByVal StartDate As Date, ByVal DayToAdd As Int32) As Int32
Dim newDate As Date = StartDate
Dim OriginalDate As Date = StartDate
Dim friday_count As Integer
For value As Integer = 1 To DayToAdd
OriginalDate = OriginalDate.AddDays(1)
If OriginalDate.DayOfWeek = DayOfWeek.Friday Then
newDate = newDate.AddDays(1)
friday_count += 1
End If
Next
Return friday_count
End Function
and im adding the result of this count to my old date.
now how can i achieve last two steps ?
This returns the final date (hope it helps)
Function CountOfFriday(ByVal StartDate As Date, ByVal DayToAdd As Int32) As Date
Dim newDate As Date = StartDate
Dim OriginalDate As Date = StartDate
Dim friday_count As Integer = 0
For value As Integer = 1 To DayToAdd
OriginalDate = OriginalDate.AddDays(1)
If OriginalDate.DayOfWeek = DayOfWeek.Friday Then
Do
friday_count +=1
Loop until StartDate.AddDays(DayToAdd+friday_count)<StartDate.AddDays(friday_count*7)
Exit for
End If
Next
newDate = newDate.AddDays(DayToAdd+friday_count)
Return newDate
End Function

Using DatePicker to get the current day within the year in VB.NET?

I am currently programming a new Windows Phone Application and within this app i want the user to be able to find the hex date for any date of their choice. For this reason i am letting the user use a date picker to choose their desired date. Before i included the date picker i was allowing the user to get the hex date for today's date by using the following code:
ReverseString = 365 - DateTime.Today.DayOfYear
txtReverseJulian.Text = ReverseString.PadLeft(3, "0")
However now i can't use the DateTime function because i am letting them choose a date from the date picker, how can i change this code so that it will work?
Thank you
I am guessing at what you are trying to do... Could you provide clarification?
Dim foo As DateTime = New DateTime(DateTime.Today.Year, 12, 31) 'last day of the current year
Dim ldoy As Integer = foo.DayOfYear 'number of days in year
Dim dr As Integer = ldoy - DateTime.Today.DayOfYear 'number of days until end of year
Using a DatePicker
Dim foo As DateTime = New DateTime(aDatePicker.Value.Year, 12, 31) 'last day of the current year
Dim ldoy As Integer = foo.DayOfYear 'number of days in year
Dim dr As Integer = ldoy - aDatePicker.Value.DayOfYear 'number of days until end of year
This may help you
//Here is event fire when datepicker value will be change
ValueChanged="DatePicker_ValueChanged"
Private Sub DatePicker_ValueChanged(sender As Object, e As DateTimeValueChangedEventArgs)
ReverseString = e.NewDateTime.Value.ToString()
End Sub
txtReverseJulian.Text = ReverseString.PadLeft(3, "0")

Get row index if some column value is equal to something

In this datatable there are no duplicates, I need the row index where column x value equals 2. I would do it like this:
Dim rowIndex As Integer = 0
For i = 0 To mtable.Rows.Count - 1
If mtable.Rows(i)("x") = 2 Then
rowIndex = i
Exit For
End If
Next
I will be calling this process multiple times per second. Is there a faster way to do this in .NET?
DataTable select could work, i think it should be faster than iterating over the collection of rows.
var index = mtable.Rows.IndexOf(mtable.Select("x = 2").FirstOrDefault());
Multiple times per second is a bit vague - tens or thousands?
You could create a hash table mapping the value of "x" to the row number:
Dim nLookups = mtable.Rows.Count - 1
Dim lookupHash As New Hashtable(nLookups)
For i = 0 To nLookups
lookupHash.Add(CInt(mtable.Rows(i)("x")), i)
Next
then
Dim rowSought As Integer = -1
If lookupHash.ContainsKey(2) Then
rowSought = lookupHash(2)
End If
Or if the range of possible values of "x" is suitable, you could use an array to map the value to the row number.