繁体   English   中英

从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);

人类:

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;}
}

样本输出:

在此处输入图片说明 在此处输入图片说明

不使用foreach获取属性

有多种方法可以枚举C#中的任何数据收集,例如doforforeach in和, 而while都是特定语言的。

同样,自.Net 3.5(大约自2008年以来)开始,添加了一些扩展,这些扩展可处理C#中的列表处理,即语言集成查询(LINQ) ,这是用于查询和更新数据的易于学习的标准模式。 术语查询是一种写表达式的能力,该表达式可以从数据源检索数据,而不管该源是本地的(对于诸如数组或列表中的代码而言)还是数据库中的数据。

请参阅C#中的LINQ入门,这是Linq的一个很好的介绍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM