C# ListView 与 ListBox 的多列使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9743193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListView vs. ListBox for multiple column usage
提问by JavaCake
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.
我目前正在为我的应用程序的 GUI 苦苦挣扎。我很难弄清楚 ListBox 还是 ListView 是否更“适合”数据的多列表示。
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?
ListBox 和 ListView 如何处理多列?
采纳答案by Bryan Crosby
There's certainly nothing wrong with a DataGridViewin this scenario.
DataGridView在这种情况下a 肯定没有错。
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
一个函数将数据加载到 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.
当然,从这里开始事情会变得更复杂,但是如果您只是想以表格方式显示数据......这很简单。
回答by Haedrian
ListView is much better for multi-column representation of data. However it seems to get more complicated/ugly code than a simple ListBox.
ListView 对于数据的多列表示要好得多。然而,它似乎比简单的 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.
我不认为 ListBox 有多个列,所以你必须破解一些丑陋的东西。
回答by om471987
Check this out
看一下这个
https://stackoverflow.com/a/227355/988830
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.
尽管 listbox 用于单列,而 listview 用于多列,但答案是这一切都取决于。
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.
有时您可能需要多列列表,您需要在其中添加不同类型的子项。您不能使用 listview 绑定它们,因此在这种情况下最好使用 listbox。但是,如果您想使用标题对它们进行排序,请务必使用 listview,因为它很简单。
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.
总之,我会说,如果你只有多列数据,如果你想做按钮、树视图、扩展器等花哨的东西,那么使用 listview 没有什么更好的了。 ListBox 真的很酷。
Thanks, Omkar
谢谢,奥姆卡
回答by Dave Cousineau
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.
如果您希望能够直接从网格编辑数据(如电子表格),则 DataGridView 非常有用。详细模式下的列表视图非常适合简单地展示数据列的列表。据我所知,DataGridView 也更容易排序。
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:
用Tag = item; 您将能够通过以下方式访问所选对象:
if (mListView.SelectedIndices.Count <= 0)
return;
Item selectedItem = mListView.SelectedItems[0].Tag as Item;
if (selectedItem == null)
return;
// do something with selectedItem

