且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从WPF中的DataGrid获取所有行

更新时间:2023-10-03 19:29:34

使用 foreach 循环:

foreach(pojo p in calendarmstrDG.Items)
{
   // do something with "p", e.g. access properties: p.SerialNo 
}

DataGrid.Items集合包含类型为 object ,因此在 foreach 循环中,我们必须指定确切的类型 pojo 能够访问属性

DataGrid.Items collection contains elements of type object, so in foreach loop we have to specify exact type pojo to be able to access properties

如果您需要获取 pojo 的新列表,可以使用Linq:

if you need to get a new list of pojo, it can be done with Linq:

List<pojo> list = calendarmstrDG.Items.OfType<pojo>().ToList();