2015-08-15 79 views
0

我在Class Restaurant中有綁定列表,我需要在我的表單Form1中調用,而不使用foreach來獲取屬性。我怎麼能訪問屬性沒有foreach。那可能嗎?從bindingList中獲取屬性值c#

這裏是我的代碼:

public static BindingList<MaterijaliGrid> GetMaterijali(DataGridView dataGridView1) 
    { 
     BindingList<MaterijaliGrid> materijali = new BindingList<MaterijaliGrid>(); 
     foreach (DataGridViewRow r in dataGridView1.Rows) 
     { 
      //while (materijali.Count < 50) 
      //{ 
      materijali.Add(new MaterijaliGrid 
      { 
       Cosort = r.Cells[0].Value.ToString(), 
       Model = r.Cells[1].Value.ToString(), 
       Type = r.Cells[2].Value.ToString(), 
       Color = r.Cells[3].Value.ToString(), 
       Aantal = r.Cells[4].Value.ToString(), 

       Unit = r.Cells[5].Value.ToString(), 
       Component = r.Cells[6].Value.ToString(), 
       Aantal2 = r.Cells[7].Value.ToString(), 
       Unitcomp = r.Cells[8].Value.ToString(), 
       Opis = r.Cells[9].Value.ToString(), 
       Kleur = r.Cells[10].Value.ToString(), 
       Soort = r.Cells[11].Value.ToString(), 
       Price = r.Cells[12].Value.ToString(), 
       Price1 = r.Cells[13].Value.ToString(), 
       Price2 = r.Cells[14].Value.ToString(), 
       // Oznaka = "MTK" 
      }); 
     } 
     //} 
     return materijali; 
    } 
+0

此代碼尖叫:「NullReferenceException」 –

+0

@Yosi becouse值是從excel文件 – user4861279

+0

設置的爲什麼你不想使用foreach?它存在的原因。 – Steve

回答

1

如果你想更清潔的代碼,我建議你應該將對象綁定到你的DataGridView。然後,轉換會很容易。事情是這樣的:

// Replace list of person with your MaterijaliGrid object 
var list = new List<Person>(); 
list.Add(new Person { FirstName = "Robert", Initial = "Santos", LastName = "Lee" }); 
list.Add(new Person { FirstName = "Robert1", Initial = "Santos1", LastName = "Lee1" }); 
list.Add(new Person { FirstName = "Robert2", Initial = "Santos2", LastName = "Lee2" }); 
list.Add(new Person { FirstName = "Robert3", Initial = "Santos3", LastName = "Lee3" }); 

// You can hide row header if don't want it. 
dataGridView1.RowHeadersVisible = false;  
dataGridView1.AutoGenerateColumns = true; 
dataGridView1.AutoSize = true;    
dataGridView1.DataSource = list; 

現在您可以輕鬆地投它是這樣的:

// Replace List and BindingList of person with your MaterijaliGrid object 
var list = new List<Person>(); 
list.AddRange(dataGridView1.Rows 
          .Cast<DataGridViewRow>() 
          .Select(row => row.DataBoundItem as Person)); 

var bindingList = new BindingList<Person>(list); 

Person類:

public class Person 
{ 
    // In case you don't want to display class property with there original names 
    // you can annotate the property with DisplayName 

    [DisplayName("First Name")] 
    public string FirstName {get; set;} 
    public string Initial {get; set;} 
    public string LastName {get; set;} 
} 

樣本輸出:

enter image description here enter image description here

0

不使用的foreach獲得性能

有多種方式來枚舉在C#中的任何數據採集,如doforforeach inwhile是特定語言的所有。

另外,由於淨3.5(〜2008年以來大致)有擴展其中加入處理列表處理在C#即Language-Integrated Query (LINQ)哪種標準,容易學習的圖案爲查詢和更新數據。術語查詢是能夠編寫一個從數據源檢索數據的表達式,而不管該數據源是本地數據還是數據庫中的數據。

請參閱Getting Started with LINQ in C#這是對Linq的一個很好的介紹。