我在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;
}
此代码尖叫:“NullReferenceException” –
@Yosi becouse值是从excel文件 – user4861279
设置的为什么你不想使用foreach?它存在的原因。 – Steve