Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate Listview from Datatable

Posted on 2010-09-05
9
Medium Priority
?
1,160 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
Independent Software Vendors: 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!

 
LVL 64

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 64

Accepted Solution

by:
Fernando Soto earned 1000 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 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

927 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