2016-11-05 69 views
0

我使用C#適配器方法,我添加元素的列表之後,我調用列表填充列表框,這裏是我的按鈕做:排序C#ListBox的項目

private void button1_Click(object sender, EventArgs e) { 
    listCustomers.Items.Clear(); 
    List <Customer> customers = CustomerList.GetCustomers(); 
    List <CustomerSaldo> customersSaldo = CustomerListSaldo.GetCustomers(); 
    int total = 0; 
    int totalCustomer = 0; 
    foreach(Customer customer in customers) 
    total++; 

    listCustomers.Items.Clear(); 

    totalCustomer = total; 
    int x = totalCustomer; 

    foreach(CustomerSaldo customer2 in customersSaldo) { 
    if (x >= 1) { 
     listCustomers.Items.Add("Costumer ID # " + x + " is: " + customer2.Saldo); 
     x--; 

    } 
    } 

} 

Result

這是我得到的,它是確定,但我想知道是否存在的方式來做到這一點類似這樣的例子:

負荷消費#1 Saldo ...
負荷消費#2 Saldo ...
負荷消費#3 Saldo ...

如果你看到我的代碼,我有一個變量x,這個變量的costumers的總數,所以我認爲我的排序已開始與此變量,但我不知道該怎麼做,我該怎麼辦?

+0

使用排序依據.... –

+0

@viveknuna排序依據? SQL?我沒有使用SQL,只有C#列出 –

+0

'OrderBy',認真嗎?他正在減少,你建議OrderBy? W0 – Tinwor

回答

1

反向列表,並開始計時,直到你到達終點:

//Reverse the list 
customersSaldo.Reverse(); 
//Add new items 
for(int i = 0; i < customers.Count; i++) 
    listCustomers.Items.Add(string.Format("Costumer ID # {0} is: {1}", (i+1).ToString(), customersSaldo[i].Saldo); 
+0

完美無缺!謝謝 –