Question Fill Listview with list of date in a month

sukarso

Member
Joined
Oct 15, 2013
Messages
6
Programming Experience
3-5
Hi, All.
I need to update Listview with dates in a month when user click a button. I have DateTimepicker and a button. Problem is how to fill each row with date.
Here is the code that i have done so far:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim l As New ListViewItem
        Dim numdays As Integer
        numdays = DateTime.DaysInMonth(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month)


        ListView1.Items.Clear() ' 
        For i As Integer = 1 To numdays
            l = ListView1.Items.Add(i)
            ' here is what i need
        Next

    End Sub
listviewdate.jpg
Thanks and regards.
 
Last edited:
Hi and welcome to the Forum,

When posting to the Forum please do remember to add comments on any errors that you may be getting so that we can help you better.

In your case there is no Overload of the Add Method of the ListView that accepts an Integer and therefore you should be getting an error telling you this. However, the first Overload of the Add Method does accept a single String value. So, add Strings and not Numbers.

Hope that helps.

Cheers,

Ian
 
Hi again,

I have just realised that this works fine with Option Strict Off. So, two further things for you:-

1) Turn Option Strict On. This will help you to identify and correct all Type Conversion errors as you encounter them within your coding. To change this in your current project go to Project->Properties on the IDE's Menu and change the options on the Compile Tab and for all future projects you can do this by going to Tools->Options on the IDE's Menu and changing the VBDefaults.

2) On the basis that this does work fine with Option Strict Off, what is the actual issue that you are having, since even though my first post was valid, I will not have addressed whatever the issue is that you are having.

Cheers,

Ian
 
I must be having a bad day since I now seem to understand that first question? Try this:-

For i As Integer = 1 To numdays
  Dim LVI As New ListViewItem
  With LVI
    .Text = i.ToString
    .SubItems.Add(New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, i).ToShortDateString)
  End With
  ListView1.Items.Add(LVI)
Next


Hope that helps and I am going for a Lie down!

Cheers,

Ian
 
Back
Top