Solved

Populate Listview from Datatable

Posted on 2010-09-05
9
1,157 Views
Last Modified: 2013-12-17
I am trying to populate a listview from a stored procedure, I can verify all data are being returned just fine but not populated properly in the listview.
I am trying to display 3 columns in my listview, but instead only the first column is showing.

here is my code:


Those are the only records in my table and the represent the ItemNo.
1- Anyone knows what wrong with the code above and not the Name and Qty fields are not showing the listview?
2- How can get rid of the ( ListViewItem: ) caption/header showing in the listview above?

Thanks
DataTable dt = ds.Tables[0];

                // Clear the listview control
                lstItems.Items.Clear();
                lstItems.MultiColumn = true;

                lstItems.BeginUpdate();

                // Display items in the ListView control
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    // Only row that have not been deleted
                    if (dr.RowState != DataRowState.Deleted)
                    {
                        // Define the list items
                        ListViewItem lvi = new ListViewItem(dr["ItemNo"].ToString()); //SHOWS
                 
                        lvi.SubItems.Add(dr["Name"].ToString());  //DOES NOT SHOW
                        lvi.SubItems.Add(dr["Qty"].ToString());     //DOES NOT SHOW


                        // Add the list items to the ListView
                        lstItems.Items.Add(lvi);

                    }

                }
                lstItems.EndUpdate();

What the code above shows when running it is the following:
ListViewItem: {12345}
ListViewItem: {56789}
ListViewItem: {12}

Open in new window

0
Comment
Question by:smaguire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33607035
lstItems.DataSource = dt;
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33607050
0
 

Author Comment

by:smaguire
ID: 33607102
I already tried to bind its datasource = dt but it didn't work as the result showing are

System.Data.DataRowView

Instead of actual values.
I am interested in knowing why my code above doesn't show all columns and only shows one column (one value). All values (rows) are showing properly in the datarow
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33607123
Hi smaguire;

In order to display columns you need to set the ListView to Detail view, so try making the following change:

// Clear the listview control
lstItems.Items.Clear();
lstItems.MultiColumn = true;
// Set to details view.
lstItems.View = View.Details;

I think that should get it to work for you.

Fernando
0
 

Author Comment

by:smaguire
ID: 33607132
Fernando,

I don't have the .View option! I am using ListView version 4.0.0.0 but no .View property is available to me.


0
 

Author Comment

by:smaguire
ID: 33607322
Fernando,

I was able to get the .View property to display, I had to remove and then add the reference pointing to it and the property showed just fine, however now it doesn't display anything.

The list is not populated. any thoughts? Is there a better way populating a listview from a stored procedure?

Thanks
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 33607848
Hi smaguire;

Sorry I left home after posting my last post and forgot to also include adding column headers which is also needed when using Detail view. In the below statements the second parameter is the initial column width, you can leave it out be the default is always not wide enough. You can set it to the widest you think you need.

// Clear the listview control
lstItems.Items.Clear();
// Set to Datail View    
lstItems.View = View.Details;
// Define the columns
lstItems.Columns.Add("Item No", 75);
lstItems.Columns.Add("Name", 75);
lstItems.Columns.Add("Qty", 75);

Fernando
0
 

Author Closing Comment

by:smaguire
ID: 33617081
Thanks, that was it
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33617160
Not a problem, glad I was able to help.  ;=)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question