none
adding row in datagridview using datetimepicker RRS feed

  • Question

  • hello is it possible to add row in datagridview using two datetimepicker?

    what i want to do is to calculate the number of months between the 2 datetimepicker

    for example i set my DTP1 to November 1 2018 and my DTP2 is April 30 2019 and we would say 30 days per month

    the datagridview will display/add 7 rows 

    Friday, November 30, 2018 2:40 PM

Answers

  • Hi

    Here is some code that may help. To try it, start a new project and add DataGridView1, DateTimePicker1, DateTimePicker2 and Button1 to the Form1 in the Designer. Then copy/replace all the code below in Form1.

    Note: I have assumed start and end dates are INCLUSIVE, if you want to EXCLUDE the end date then remove the highlighted portion of:

    Dim days As Long = DateDiff(DateInterval.Day, DateTimePicker1.Value,
    DateTimePicker2.Value.AddMinutes(1))

    Image

    Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
        End With
        With DateTimePicker2
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
        End With
        With DataGridView1
          .Columns.Add("ID", "ID")
          .Columns.Add("Date", "Date")
    
          .Columns("ID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Date").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim days As Long = DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value.AddMinutes(1))
    
        For i As Long = 0 To days
          DataGridView1.Rows.Add(DataGridView1.NewRowIndex + 1, DateTimePicker1.Value.AddDays(i).ToString("ddd, dd MMM yyyy"))
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, November 30, 2018 3:53 PM
    • Marked as answer by no[one] Saturday, February 23, 2019 10:27 AM
    Friday, November 30, 2018 3:49 PM