2015-08-15 74 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的一个很好的介绍。