ListView vs. ListBox for multiple column usage

前端 未结 4 1143
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 13:11

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

相关标签:
4条回答
  • 2021-02-02 13:20

    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
    
    0 讨论(0)
  • 2021-02-02 13:34

    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

    0 讨论(0)
  • 2021-02-02 13:35

    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.

    0 讨论(0)
  • 2021-02-02 13:37

    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

    0 讨论(0)
提交回复
热议问题