none
Display specific dates within a date range RRS feed

  • Question

  • Hello,

    I have a VB form with 2 datetimepicker componenets (d1, d2), 7 checkboxes for the days of the week and one datagridview

    I need to be able to specify a date range (ie from 2017-01-01 to 2017-01-31) and select the days of the week (ie Monday and Wednesday based on the  checkboxes) and the datagridview will be filled with all the Mondays and Wednesdays of that date range.

    Please, I really appreciate the help since I am not that strong with date functionalities in VB.NET.

    Thank you very much,

    ITJunior

    Sunday, May 21, 2017 6:44 PM

All replies


  • Please, I really appreciate the help since I am not that strong with date functionalities in VB.NET.

    Thank you very much,

    ITJunior

    Then try it in the VB forum

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral


    Success
    Cor

    Sunday, May 21, 2017 8:21 PM
  • Hi ITJunior,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the VB development. Since our forum is to discuss the VS IDE, I will help you move the thread to the appropriate forum: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral for dedicated information, thank you for your understanding.

    Best regards,

    Sara


    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.

    Monday, May 22, 2017 2:36 AM
  • I need to be able to specify a date range (ie from 2017-01-01 to 2017-01-31) and select the days of the week (ie Monday and Wednesday based on the  checkboxes) and the datagridview will be filled with all the Mondays and Wednesdays of that date range.

    Use the DateAdd method of the datetime object to increment the date by one day and then check that date for day of the week.  If it's a day of the week that corresponds to one of the checked checkboxes then add it to the collection of selected dates.  Repeat until you get to the end date.

    DateAdd: https://msdn.microsoft.com/en-us/library/c0ccadae(v=vs.110).aspx

    Day of week: https://msdn.microsoft.com/en-us/library/system.datetime.dayofweek(v=vs.110).aspx

    Monday, May 22, 2017 8:52 AM
  • Hi AL5700,

    According to your description, you can use sql to query for a period of time datafirstly, and then add one field to display weekday.

    Private Sub loaddate()
            Dim dt As New DataTable()
            Dim strConn As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Demo\Data3.mdf;Integrated Security=True"
            Using conn As New SqlConnection(strConn)
                Dim selectSql = "select *,(select datename(dw,applytime)) as whatday from test2 where applytime between @Fromtime and @Totime "
                Using com As New SqlCommand(selectSql, conn)
                    Try
                        conn.Open()
                        com.Parameters.AddWithValue("@Fromtime", DateTimePicker1.Value)
                        com.Parameters.AddWithValue("@Totime", DateTimePicker2.Value)
                        Dim adapter As New SqlDataAdapter(com)
                        adapter.Fill(dt)
                        'Dim dv As DataView = dt.DefaultView
                        'dv.RowFilter = "where whatday = '" & CheckBox1.Text + "'"
                        'DataGridView1.DataSource = dv
    
                        DataGridView1.DataSource = dt
                        conn.Close()
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString())
                    End Try
                End Using
            End Using
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            loaddate()
        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.



    Monday, May 22, 2017 9:54 AM
    Moderator
  • AL5700,

    You might want to rethink the controls you're using.

    A MonthCalendar sounds to be more in line for what you want:

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

    Specifically the .SelectionRange property:

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


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

    Monday, May 22, 2017 10:18 AM