C# 在C中向listview添加列#
我在SearchFly类中有一个Arraylist GetFly() 我希望在Form1.cs中按[zbor(colZbor)、airport(colAirport)、company(colCompany)]列列出listview中的列表,但我现在不知道该怎么做C# 在C中向listview添加列#,c#,listview,arraylist,C#,Listview,Arraylist,我在SearchFly类中有一个Arraylist GetFly() 我希望在Form1.cs中按[zbor(colZbor)、airport(colAirport)、company(colCompany)]列列出listview中的列表,但我现在不知道该怎么做 private SearchFlyClass searchFly = new SearchFlyClass(); private ArrayList fly = new ArrayList(); ... private void Sho
private SearchFlyClass searchFly = new SearchFlyClass();
private ArrayList fly = new ArrayList();
...
private void ShowResultFlySearch(int direction, string country)
{
fly = searchFly.GetFly(direction, country);
for (int count = 0; count < fly.Count; count++)
{
string zbor = fly[0].ToString();
string companie = fly[1].ToString();
string aeroport = fly[2].ToString();
ListViewItem searchlist = new ListViewItem();
searchlist.Items.Add(new ListViewItem(elem));
}
}
有人能帮我一下吗?首先,您必须将ListView设置为查看模式详细信息,您可以使用以下代码执行此操作(也可以在designer中设置View属性): 然后,您必须将列分配给listView(也可以在designer中完成): 在此之后,您必须通过修改函数将其他列指定给ListViewItem子项:
private void ShowResultFlySearch(int direction, string country)
{
fly = searchFly.GetFly(direction, country);
for (int count = 0; count < fly.Count; count++)
{
string zbor = fly[0].ToString();
string companie = fly[1].ToString();
string aeroport = fly[2].ToString();
ListViewItem listViewItem = new ListViewItem(zbor);
listViewItem.SubItems.Add(airport);
listViewItem.SubItems.Add(companie);
listView.Items.Add (listViewItem);
}
}
该函数假定它位于Form1.cs中,并且listView变量实例化为listView类型的类变量。C#和面向对象编程的基础知识 这段代码有很多问题。首先,您使用
ArrayList
而不是泛型集合类型有什么原因吗?例如,列表
其次,我将创建一个类型来存储实体的一个实例的所有相关数据,而不是将实体的列值放入非类型化集合
第三,您没有在for
循环中的任何位置引用count
——可能是因为查询返回单个实体,因此for
循环是多余的,因为您知道为单个实体返回的项数。您还使用了一个似乎尚未定义的变量elem
已更新
定义描述实体的类型:
public class Flight
{
public decimal Code { get; set; }
public string Company { get; set; }
public string Airport { get; set; }
}
public Flight GetFlight(int tip, string country)
更改方法以返回实体的实例:
public class Flight
{
public decimal Code { get; set; }
public string Company { get; set; }
public string Airport { get; set; }
}
public Flight GetFlight(int tip, string country)
创建要从方法返回的新实例,并从数据库查询结果填充它:
var flight = new Flight();
flight.Code = reader.GetDecimal(cod_zbor);
flight.Airport = reader.GetString(nume_aeroport);
flight.Company = reader.GetString(nume_companie);
return flight;
现在,您的其他方法可以使用更新的方法:
var flight = searchFly.GetFlight(...);
// access flight properties here
这假设查询返回单个实体。如果它返回一个集合,那么您可以使用List
或IEnumerable
作为适当的返回类型。I now List是bether,bat如何使用数据库添加列表我现在不给我举个例子
var flight = searchFly.GetFlight(...);
// access flight properties here