我在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