从 bindingList c# 中获取 属性 值
get property value from bindingList c#
我在 Class Restaurant
中有绑定列表,我需要在我的表单 Form1
中调用它而不使用 foreach 来获取属性。如果没有 foreach,我怎么能访问属性。那可能吗?
这是我的代码:
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;
}
如果您想要更简洁的代码,我建议您将对象绑定到 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);
人 class :
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;}
}
示例输出:
without using foreach to get properties
在 C# 中有多种方法可以枚举任何数据集合,例如 do, for, foreach in, and the while,它们都是特定于语言的。
此外,自 .Net 3.5(大约自 2008 年以来)以来,添加了一些扩展,用于处理 C# 中的列表处理,即 Language-Integrated Query (LINQ) 查询[=22] 的标准、易于学习的模式=] 和更新数据。术语 querying 是编写从数据源检索数据的表达式的能力,无论该数据源是本地的(对于数组或列表中的代码)还是来自一个数据库。
参见 Getting Started with LINQ in C#,这是对 Linq 的一个很好的介绍。
我在 Class Restaurant
中有绑定列表,我需要在我的表单 Form1
中调用它而不使用 foreach 来获取属性。如果没有 foreach,我怎么能访问属性。那可能吗?
这是我的代码:
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;
}
如果您想要更简洁的代码,我建议您将对象绑定到 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);
人 class :
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;}
}
示例输出:
without using foreach to get properties
在 C# 中有多种方法可以枚举任何数据集合,例如 do, for, foreach in, and the while,它们都是特定于语言的。
此外,自 .Net 3.5(大约自 2008 年以来)以来,添加了一些扩展,用于处理 C# 中的列表处理,即 Language-Integrated Query (LINQ) 查询[=22] 的标准、易于学习的模式=] 和更新数据。术语 querying 是编写从数据源检索数据的表达式的能力,无论该数据源是本地的(对于数组或列表中的代码)还是来自一个数据库。
参见 Getting Started with LINQ in C#,这是对 Linq 的一个很好的介绍。