簡體   English   中英

在C#中將列添加到ListView

[英]Add columns to listview in C#

我在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),機場(colAirport),公司(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));

                }
        }

有人能幫助我嗎?

首先,您必須將ListView置於“查看”模式詳細信息,您可以使用以下代碼進行操作(也可以通過在Designer中設置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#和面向對象編程的基礎。

此代碼有很多問題。 首先,您是否有任何理由使用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>作為返回類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM