Solved

Populate Listview from Datatable

Posted on 2010-09-05
9
1,156 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# LINQ 5 43
Need to Interpret Json in Vb.net ( .net 3.5  framework) 11 51
Unprotect Visio drawing page in C# 4 22
Subtract dates in vb.net 6 34
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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