Solved

Populate Listview from Datatable

Posted on 2010-09-05
9
1,155 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

713 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