Hello All. This is my first post here, and I'm hoping that it's my first of many.
I'm just starting out in C#. I did a really small project in VB.NET, but other than that, my experience in any programming language is highly limited.
I'm working on a project for a community of hobbyists who have released models and other media for the public's usage. I've gone through and categorized these (and am still in the process of doing so) into an Access database. There are about 5 categories that help "tag" the entry, and the other 2 are for the download link and a screenshot of it... Anyway... All that aside...

I really have 3 questions. There are about a thousand entries in this database, yet I want to have it to when you click on an entry in the browser, it opens a form with the download link and screenshot. Is it possible to do that with a ListView? And is the ListView the best way to display all of the data?

Lastly,

private ListView FillListView(ListView lv)
        {
           OleDbDataReader reader;
            string strCommand = "SELECT * FROM Media;";
          //database
           OleDbCommand cmd = new OleDbCommand(strCommand, conConnection);
           reader = cmd.ExecuteReader();
           while (reader.Read())
          {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = (reader["ID"].ToString());
                li.SubItems.Add(reader["Name"].ToString());
                li.SubItems.Add(reader["Creator"].ToString());
                li.SubItems.Add(reader["Download Link"].ToString());
                li.SubItems.Add(reader["Category"].ToString());
                li.SubItems.Add(reader["Genre"].ToString());
                li.SubItems.Add(reader["Screenshot"].ToString());
                li.SubItems.Add(reader["Date Added"].ToString());
                lv.Items.Add(li);
            }
            reader.Close();
          return (lv);
           
       }

This code works perfectly, and fills the ListView exactly as I want it. However, only the first column, the "ID" column, is clickable. Is there some way to make all of the other columns clickable in the same way? I already tried li.SubItems[1].Text = (reader["Name"].ToString()); as an option, but it gave me an error during runtime saying that it doesn't have an index of 1. I've spent a couple hours tinkering with the code trying to figure it out, but have come to no means of solving it. Is it possible that I can get some help, not only with getting the code to work out the way I'd like it to, but explaining why those changes need to be made?

Thank you very much in advance,
Gunn3r

Recommended Answers

All 4 Replies

In your case use DataGrid, it's most suitable to your case.

I think you mean something like this:

ListViewItem [] li = new ListViewItem[4]; //define an array for 4 ListViewItems

            //fill the array up
            //just using strings here instead of your reader
            li[0] = new ListViewItem("ID");
            li[1] = new ListViewItem("Name");
            li[2] = new ListViewItem("Creator");
            li[3] = new ListViewItem("Download Link"); 
            // ...
            //Add the array to the Items collection of your listview
            lv.Items.AddRange(li);

Perhaps do something likewise for the subitems?
The reason your indexing didn't work is: you allocated a ListViewItem with a starting address for the SubItems in it ( [0] ) but you never allocated a list of subitems so [1] fails.

My post came after Ramys. He is right, you should use a DataGridView here. Just was poinitng out some ListView thingies.

Thanks for the help. I've switched it to a DataGridView, and everything seems to be working much better. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.