19

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?

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
JavaCake
  • 4,075
  • 14
  • 62
  • 125
  • @BryanCrosby, im working with List data right now. I understand that the DataGridView is suiteable for database data. – JavaCake Mar 16 '12 at 20:23
  • You said you have multi-column representation. That's a table, which is why I recommended it. What is your data structure? – Bryan Crosby Mar 16 '12 at 20:51
  • @BryanCrosby, my data is represented as a List right now. – JavaCake Mar 16 '12 at 21:31
  • Datagridview is suitable for any kind of data that you wish to display in a grid. I use list-controls when I can because they are lighter but when you need flexibility and multi-column presentation for your data datagridview is hard to beat. – ThunderGr Feb 04 '14 at 08:05

4 Answers4

12

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.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • 1
    Perfect. Exactly what i was looking for. Only had to add one line of code which initialized the datasource and it did the magic. – JavaCake Mar 17 '12 at 09:27
  • 1
    Is it possible to exclude any column or change the order of them? – JavaCake Mar 17 '12 at 15:08
  • 2
    @JavaCake: You can access the columns with their named index or ordinal. `DataGridView.Columns[0]`, for example. There is a property `DisplayIndex` which controls how they can be displayed. There is also a `Visible` property which controls visibility. – Bryan Crosby Mar 17 '12 at 16:14
4

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

Community
  • 1
  • 1
om471987
  • 5,398
  • 5
  • 32
  • 40
4

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
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
3

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

Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • 1
    All the examples i could find on both types end up in somewhat messy code. But as you say the ListView seems much more suiteable. Can you give an codesnippet on a multicolumn ListView? Thanks. – JavaCake Mar 16 '12 at 19:21
  • 1
    The ListBox control certainly does support [multiple columns](http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.multicolumn.aspx). – M.Babcock Mar 16 '12 at 19:22
  • 1
    "A multicolumn ListBox places items into as many columns as are needed to make vertical scrolling unnecessary." I don't think its that helpful. – Haedrian Mar 16 '12 at 19:24
  • http://www.xtremedotnettalk.com/showthread.php?t=93443 That has a code sample. I don't have my code here otherwise I'd give you something from one of my projects – Haedrian Mar 16 '12 at 19:25