我一直在寻找一些方法来使用DataTables并使用自定义类获取她的行的值。典型的DataTable使用自定义类获取她的行值
我的做法是这样的:
DataTable dtUsers = new DataTable();
dtUsers.Columns.Add("Name");
dtUsers.Columns.Add("Last name");
dtUsers.Columns.Add("Age", typeof(int));
for (int x = 1; x <= 100; x++)
{
dtUsers.Rows.Add(new object[]
{
"Name " + x,
"Last name " + x,
x
});
}
// I want to do this
var Name = dtUsers.Rows[0].Name; // Get "Name 1"
var Age = dtUsers.Rows[50].Age; // Get "Age 50"
// Or
foreach(DataRow drCurrent in dtUsers.Select("Age > 50"))
{
int Age = drCurrent.Age; // Get 51,52,53..
}
我想是这样的:
public class DataTablePerson : DataTable
{
public string Name { get; set; } // Implement a method to return a specific value in some specific row
public string Last_name { get; set; } // Implement a method to return a specific value in some specific row
public int Age { get; set; }// Implement a method to return a specific value in some specific row
}
的存在是为了实现这一目标的方法?
如果我可以使用自定义类作为参数some typized class来重新应用这个类的其他结构(例如:Person,Tree,Movie,Book),那么这将是有用的。
谢谢,但; 1)这是正常的方法(我想知道源中的现有列)。 2和3)我会得到一个特定列的值;我想拥有DataRow的所有值。 – MiBol 2015-03-19 01:29:11