将集合转变为数据网格
所以我现在在列表框中有一个集合,它显示集合中的每个项目。我想将列表框变成网格视图,这样我就可以添加复选框和其他下拉菜单。我无法找到解释如何执行此操作的教程。
对于示例,我的集合有 2 个项目,每个项目都有多个列。
- [0] 747 喷气式飞机
- [0]乘客数量:417
- [1] 首次飞行:1967 年
- [0] A380
- [0]乘客数量:853
- [1] 首次飞行:2005 年
现在我的列表框使用此代码
foreach (AMAPnr.Airplane airElement in AMAPnr.Airplanes)
{
lstPlanes.Items.Add(
"PassengerAmount: " + airElement.Passenger + " First Flight:" +
airElement.FirstFlight.ToString());
}
我将如何将其更改为网格视图?
So I have a collection that I have in a listbox right now, which displays each item in the collection. I want to turn the listbox into a gridview, so I can add checkboxes and other drop downs. I am unable to find a tutorial that explains how to do this.
For the example my collection has 2 items, each item has multiple columns.
- [0] 747 Jet
- [0] Passenger amount: 417
- [1] First Flight: 1967
- [0] A380
- [0] Passenger amount: 853
- [1] First Flight: 2005
Right now my listbox uses this code
foreach (AMAPnr.Airplane airElement in AMAPnr.Airplanes)
{
lstPlanes.Items.Add(
"PassengerAmount: " + airElement.Passenger + " First Flight:" +
airElement.FirstFlight.ToString());
}
How would i go about changing this into a gridview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:OP 已澄清他们选择了错误的标签,这实际上是针对 WinForms 的。
如果您将
DataGridView
添加到表单中,然后将以下代码放入您的表单中,形成代码隐藏,它的工作原理是一种享受:我使用了一个自定义的 Airplane 类来展示这个示例,我不确切地知道你的代码的结构,这样对我来说更容易。您应该能够相对轻松地插入自定义数据类型。
Update: The OP has clarified that they chose the wrong tags and this was actually for WinForms.
If you add a
DataGridView
to your form, and then put the following code in your forms codebehind, it works a treat:I've used a custom Airplane class to show this example a I don't know precisely how your code's structured and it was easier for me that way. You should be able to plug in your custom datatype relatively easily.
然后只需执行
myDataGrid.DataSource = getItems(AMAPnr.Airplanes);
您也可以执行
myDataGrid.DataSource = lstPlanes;
Then just do
myDataGrid.DataSource = getItems(AMAPnr.Airplanes);
You can also just do
myDataGrid.DataSource = lstPlanes;