populating a ListView from a method?

I have the following code that with the 2 commented out lines will populate a listview from an sql query in the code.  I'm looking to put this method into a DB class so I only want the 2 commented out lines in my windows form. I tried passing "liv" used to make the columns as shown below. How do I write the header to return a listView  to populate my listView in my form ? Thanks!
public static ListViewItems LoadDataInListView(ListViewItems liv)
        {
            SqlConnection con = new SqlConnection("Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True");
            SqlCommand comm = new SqlCommand("SELECT * FROM MemberBookInventory", con);
            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            //listView1.Items.Clear();
            while (reader.Read())
            {
                string[] data = new string[8];
                data[0] = reader["Book"].ToString();
                data[1] = reader[""].ToString();
                data[2] = reader[""].ToString();
                data[3] = reader[""].ToString();
                data[4] = reader[""].ToString();
                data[5] = reader[""].ToString();
                data[6] = reader[""].ToString();
                data[7] = reader[""].ToString();
                ListViewItem liv = new ListViewItem(data);
               // listView1.Items.Add(liv);
            }
            con.Close();
        }

Open in new window

linkcube1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Does this do what you wanted ?

public LoadListView()
{
LoadDataInListView(listView1);
}

public static void LoadDataInListView(ListView lv)
        {
            lv.Items.Clear();
            SqlConnection con = new SqlConnection("Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True");
            SqlCommand comm = new SqlCommand("SELECT * FROM MemberBookInventory", con);
            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                string[] data = new string[8];
                data[0] = reader["Book"].ToString();
                data[1] = reader[""].ToString();
                data[2] = reader[""].ToString();
                data[3] = reader[""].ToString();
                data[4] = reader[""].ToString();
                data[5] = reader[""].ToString();
                data[6] = reader[""].ToString();
                data[7] = reader[""].ToString();
                ListViewItem liv = new ListViewItem(data);
               lv.Items.Add(liv);
            }
            con.Close();
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
linkcube1Author Commented:
Exactly what I wanted thanks
0
daveamourCommented:
Whilst this works, it breaks some fundamental OO principles.

Let me know if you are interested in a more elegant solution.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

linkcube1Author Commented:
Yeah, sure.
0
daveamourCommented:
Ok can I first check why items 1..7 are reader[""].ToString() - ie empty string - should they all be "Book"?
0
linkcube1Author Commented:
Yeah, I filled in all the columns I needed so book is actually replaced by something else.. I just threw that in there.  I'll post my actual code in a half hour.
0
linkcube1Author Commented:
Here
public static void LoadDataInListView(ListView lv)
        {
            lv.Items.Clear();
            SqlConnection con = new SqlConnection("Data Source=localhost\\SqlExpress;Initial Catalog=MMABooks;" + "Integrated Security=True");
            SqlCommand comm = new SqlCommand("SELECT * FROM MemberBookInventory", con);
            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                string[] data = new string[8];
                data[0] = reader["BookID"].ToString();
                data[1] = reader["MemberID"].ToString();
                data[2] = reader["ISBNNumber"].ToString();
                data[3] = reader["Title"].ToString();
                data[4] = reader["Author"].ToString();
                data[5] = reader["Description"].ToString();
                data[6] = reader["Condition"].ToString();
                data[7] = reader["Price"].ToString();
                ListViewItem liv = new ListViewItem(data);
                lv.Items.Add(liv);
            }
            con.Close();
        }

Open in new window

0
daveamourCommented:
Does this actually work then or do you only see the BookID in the list?
0
linkcube1Author Commented:
Works fine actually.
0
daveamourCommented:
Can you show me a screenshot of the listview populated?
0
linkcube1Author Commented:
sure,
Capture.PNG
0
daveamourCommented:
Hi

My whole aproach to this was based on the assumption that you can data bind a lisview.  Turns out you can't so just completley forget what I've said!

Many apologies
0
linkcube1Author Commented:
not a problem!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.