ListView vs. ListBox for multiple column usage

前端 未结 4 1154
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  旧时难觅i
    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
    

提交回复
热议问题