C# 如何获取列表中的datagrid行值

C# 如何获取列表中的datagrid行值,c#,C#,这是我的类,包含我的DataGrid,“calendarmstrDG”的行详细信息 public class pojo { public string Prefix { get; set; } public int Year { get; set; } public int Quarter { get; set; } public int SerialNo { get; set; } public string From { get; set; }

这是我的类,包含我的
DataGrid
,“calendarmstrDG”的行详细信息

public class pojo
{
    public string Prefix { get; set; }
    public int Year { get; set; }
    public int Quarter { get; set; }
    public int SerialNo { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public string PeriodName { get; set; }
}
我使用下面的代码从
DataGrid
获取行数据:

var rowdata = calendarmstrDG.SelectedItem as pojo;
如何将行数据转换为值列表?

引用和

按如下方式设置
rowdata

pojo rowdata = calendarmstrDG.SelectedItem as pojo;
循环浏览属性:

List<string> properties = new List<string>();

foreach (PropertyInfo propertyInfo in rowdata.GetType().GetProperties())
{
   properties.Add(propertyInfo.GetValue(rowdata,null).ToString());
}
列表属性=新列表();
foreach(rowdata.GetType().GetProperties()中的PropertyInfo PropertyInfo)
{
Add(propertyInfo.GetValue(rowdata,null.ToString());
}

这将为您提供一个包含对象属性值的字符串格式的
列表。

var只是一种简写语法。该变量的实际类型可以是任何类型。在这种情况下,您(通过
as
)断言它是一个
pojo
。什么是
pojo
?那是正确的类型吗?日历对象的
SelectedItem
属性返回的类型是什么?
var
不是
ArrayList
。它是
pojo
null
的一个实例。除非您真正想要的是calendarmstrDG.SelectedItems???否则,无论您在这里做什么,都不要再使用
ArrayList
,使用
列表
(并命名您的类
pojo
),这样您希望行的列在数组中吗?