【问题标题】:Add columns to listview in C#在 C# 中将列添加到列表视图
【发布时间】:2012-09-30 09:05:58
【问题描述】:

我在 SearchFlyClass 中有一个 Arraylist GetFly()

        ...
        public ArrayList GetFly(int tip, string country)
        {
            ...
                    var list = new ArrayList();
                    var reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        ...
                        while (reader.Read())
                        {
                            decimal nr_zbor = reader.GetDecimal(cod_zbor);
                            string aeroport = reader.GetString(nume_aeroport);
                            string companie = reader.GetString(nume_companie);
                            list.Add(nr_zbor);
                            list.Add(companie);
                            list.Add(aeroport);
                        }
                    }
            ...

我希望通过列 [zbor(colZbor),airport(colAirport),company(colCompany)] 将列表放入 Form1.cs,但我现在不知道如何

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));

                }
        }

有人可以帮帮我吗?

【问题讨论】:

    标签: c# listview arraylist


    【解决方案1】:

    首先,您必须将 ListView 放入查看模式详细信息,您可以使用以下代码执行此操作(也可以在设计器中设置 View 属性):

    ListView listView = new ListView();
    listView.View = View.Details;
    

    然后你必须将列分配给listView(也可以在设计器中完成):

    listView.Columns.Add("zbor");
    listView.Columns.Add("airport");
    listView.Columns.Add("company");
    

    在此之后,您必须通过修改您的函数将其他列分配给 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# 和面向对象编程的基础知识。

    【讨论】:

      【解决方案2】:

      这段代码有很多问题。首先,您是否有任何理由使用 ArrayList 而不是通用集合类型?例如。 List<T>

      其次,我会创建一种类型来存储实体的一个实例的所有相关数据,而不是将实体的列值放入无类型集合中。

      第三,您没有在 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)
      

      创建一个新实例以从该方法返回并从您的数据库查询结果中填充它:

      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<Flight>IEnumerable<Flight> 作为您的返回类型。

      【讨论】:

      • 我现在 List 是 bether,蝙蝠如何使用数据库添加到列表中,我现在不给我一个例子
      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多