Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView vs. ListBox for multiple column usage

I am currently struggling with the GUI of my application. I have a hard time figuring out whether the ListBox or ListView is more "suitable" for multi-column representation of data.

I prefer "clean" code that is not too confusing to figure out, as spaghetti code and hacking methods can lead to confusion.

How do both ListBox and ListView handle multiple columns?

like image 945
JavaCake Avatar asked Mar 16 '12 19:03

JavaCake


4 Answers

There's certainly nothing wrong with a DataGridView in this scenario.

Sample:

class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
}

A function to load the data to the DataGridView

private void LoadData()
{
    List<Car> cars = new List<Car>()
    {
       new Car() { Make = "Subaru", Model = "Impreza", Year = 2005 },
       new Car() { Make = "Ford", Model = "Mustang", Year = 1984 }
    };

    dataGridView1.DataSource = cars;
}

Of course, from here things can get more complicated, but if you simply want to display data in a tabular fashion... it's pretty simple.

like image 118
Bryan Crosby Avatar answered Sep 27 '22 22:09

Bryan Crosby


Check this out

https://stackoverflow.com/a/227355/988830

Though listbox is used for single column and listview is used for mutlicolumn, the answer is it all depends.

Sometimes you may need multicolumn list where you need to add different types of children. You cannot bind them using listview so its better to use listbox in such scenarios. But if you want to sort them by using header, do use listview because it is simple.

In conclusion, I would say if you just have multi column data and nothing more better to use listview else if you want to do fancy stuff like buttons, treeview, expander etc. ListBox is really cool.

Thanks, Omkar

like image 22
om471987 Avatar answered Sep 27 '22 22:09

om471987


A DataGridView is nice if you want to be able to edit data straight from the grid, like a spreadsheet. A listview in detail mode is great for simple presentation of lists of data columns. A DataGridView will also be easier to sort, as far as I know.

Generally I do something like this:

private void UpdateListView()
{
   mListView.Items.Clear();
   foreach (Item item in mItems)
   {
      ListViewItem listViewItem = 
         new ListViewItem(item.Value1.ToString()) { Tag = item; }
      listViewItem.SubItems.Add(item.Value2.ToString());
      listViewItem.SubItems.Add(item.Value3.ToString());
      mListView.Items.Add(listViewItem);
   }
}

The columns will need to have been defined in the designer, including column header text and column widths.

With the Tag = item; part you will be able to access the selected object with:

   if (mListView.SelectedIndices.Count <= 0)
      return;

   Item selectedItem = mListView.SelectedItems[0].Tag as Item;
   if (selectedItem == null)
      return;

   // do something with selectedItem
like image 42
Dave Cousineau Avatar answered Sep 27 '22 23:09

Dave Cousineau


ListView is much better for multi-column representation of data. However it seems to get more complicated/ugly code than a simple ListBox.

Still its much better for many reasons, resizeable columns and all that.

I don't think ListBox has multiple columns so you'd have to hack something ugly in.

http://www.xtremedotnettalk.com/showthread.php?t=93443

like image 35
Haedrian Avatar answered Sep 27 '22 22:09

Haedrian