Filling ListView Dynamically From any Data Sources with VB.NET

Filling ListView Dynamically from Any Data Sources with VB.NET

When I was developing different front-end data entry modules for data entry operators, I found that it is very common in different modules to fill a listview according to given query strings, but I was designing a listview for each module from scratch. Then, I thought to make a user control that just takes a connection and query strings and add columns to it dynamically and fills it in the data grid style. Thanks a lot, .NET user control features.

Then, I decided to modify this control and make it more simple, just to share an idea with you about how to fill the list view dynamically.

Let us consider some important parts of the code:

Dim c As DataColumn
For Each c In ds.Tables(0).Columns

   'adding names of columns as Listview columns
   Dim h As New ColumnHeader
   h.Text = c.ColumnName
   h.Width = Convert.ToInt32(Me.numericUpDown1.Value)
   Me.listView1.Columns.Add(h)
Next

The above-mentioned code takes each column name from the query and adds it as the Listview Columns headers.

Dim dt As DataTable = ds.Tables(0)
Dim str(Me.ds.Tables(0).Columns.Count)
'adding Datarows as listview Grids
Dim rr As DataRow
For Each rr In dt.Rows
   For col As Integer = 0 To Me.ds.Tables(0).Columns.Count - 1

      str(col) = rr(col).ToString()
   Next
   Dim ii As New ListViewItem(str)
   Me.listView1.Items.Add(ii)
   'showing the number of records still added
   Me.ShowStatus()

Next

The above-mentioned code is a very important part of the code. We declare an array string equal to the numbers of columns, and then we fill this array with the help of a counter loop. Use a foreach loop to ensure that each row has been filled. So, this basic ideas control was ready to serve me.

However, when I used it with complex queries—for example, those with 8 inner joins having more than 60,000 records—it hung and did not respond, so I had to restart my system.

Then, I decided to use my programming servants (threads) to share my burden after initializing a thread loaded with database querying Hurrah!! I was jubilant now because I did not have to restart my system and I could even see adding records to listView as smoothly as it was a simple query. Thanks, threads (my friends now).

'check if thread is allow to run
   If Me.runThread = True Then
      Dim t As New Thread(AddressOf Me.FillMethod)
      t.Start()
         End If

This is a very basic way to initialize a thread and pass it the reference of a method to be called.

So, be happy, and enjoy the code.



About the Author

Ijaz Ahmed

I am Ijaz Ahmed,working as .Net Developer and Software Engineer at Lahore, Pakistan.I am BS(Honors)Computer Sciences and MCP.NET.You can contact me at NewCallForU@msn.com OR NewCallForU@Yahoo.com

Downloads

Comments

  • Bad Link

    Posted by rodrigo on 04/30/2012 08:30pm

    Bad Link

    Reply
  • Bad Link

    Posted by rodrigo on 04/30/2012 08:29pm

    the link does not work. http://www.codeguru.com/dbfiles/get_file/ListViewFillingDynamicallyVB.zip?id=7923&lbl=LISTVIEWFILLINGDYNAMICALLYVB_ZIP

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

  • Live Event Date: April 30, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT It's likely today that you'll need to recover a single database far more than you'll need to recover all of SQL Server – and yet, your backup and recovery strategy focuses on recovering the enterprise, and not daily recovery needs of that enterprise. Join Rick Vanover from Veeam, as we discuss the hype and reality of recovering your SQL databases at a granular level, tools vs. backups, where virtualization fits in, and how to successfully …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date