954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

listview in vb.net

hi. i just want to ask how can i populate a listview n vb.net that has a is limited by a date from and date to. i have a datetimepicker for start date and datetimepicker for end date and i want do list all the entry of my database in listview that is within the range of the start date and end date. thanks in advance

iefilec
Newbie Poster
5 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Are you having a problem with the SQL part, the ListView part, or both? My SQL is rusty so I'll leave that to the experts as I have no SQL implementation to play with, however, the ListView part is something I can help you with. What specifically are you having a problem with? Is it configuring the listview columns, adding the listview items, etc?

Reverend Jim
Posting Shark
Moderator
1,169 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

One of the ways is to create a SELECT command for your database with the range in the select clause.
With this command you can create a datareader to read row-by-row from your database and using each row, add a listview item (with subitems if needed) until no more data is returned from the database, then close the reader.

The select clause can be some thing like:

SELECT * FROM YourTable WHERE TheDateField BETWEEN '2011-03-10' AND '2011-03-22' ORDER BY TheDateField ASC;

In VB you can create the string for the command using:

Dim strCmd as String = "SELECT * FROM YourTable WHERE TheDateField BETWEEN '" & _
        Format(DatePickerFrom.SelectedDate,"yyyy-MM-dd") & "' AND '" & _
        Format(DatePickerTo.SelectedDate,"yyyy-MM-dd") & "' ORDER BY TheDateField ASC;


I let to you to verify that the selected to date is greater than the selected from date.

Hope this helps

lolafuertes
Master Poster
798 posts since Oct 2008
Reputation Points: 120
Solved Threads: 167
 

Then if your recordset is returned in the variable "rec" you can do

do until rec.EOF
   dim item as new ListViewItem(rec(0).Value)
   item.SubItems.Add(rec(1).Value)
   item.SubItems.Add(rec(2).Value)
   .
   .
   .
   lvwListView.Items.Add(item)
   rec.MoveNext()
loop

rec.Close()

Or you can use the field names instead if a field index, that is, rec("FIRST_NAME").Value. And forgive me if my SQL syntax is a little rusty.

Reverend Jim
Posting Shark
Moderator
1,169 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

thanks for your replies:) reverend jim, and lolafuertes.these will be helpful! thank you

iefilec
Newbie Poster
5 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
hi. i just want to ask how can i populate a listview n vb.net that has a is limited by a date from and date to. i have a datetimepicker for start date and datetimepicker for end date and i want do list all the entry of my database in listview that is within the range of the start date and end date. thanks in advance

Database is probably the cleanest way, but you could also load asll the data into an object derived from an IList, then use LINQ to limit the data, and bind that also?

MartinPlatt
Light Poster
29 posts since Sep 2011
Reputation Points: 10
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: