Solved

Populate Listview from Datatable

Posted on 2010-09-05
9
1,150 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
  • 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
 
LVL 62

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 62

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 62

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now