将特定 [Column][Row] 值放入 DataTable 的最佳方法是什么?
private DataTable CurrentTable { get; set; }
public string selectCell(int Column, int Row)
{
return CurrentTable............
}
将特定 [Column][Row] 值放入 DataTable 的最佳方法是什么?
private DataTable CurrentTable { get; set; }
public string selectCell(int Column, int Row)
{
return CurrentTable............
}
使用适当的索引器:
public string selectCell(int Column, int Row)
{
if (CurrentTable.Rows.Count <= Row) // zero based indices
throw new ArgumentException("Invalid number of rows in selectCell", "Row");
if (CurrentTable.Columns.Count <= Column)
throw new ArgumentException("Invalid number of columns in selectCell", "Column");
var row = CurrentTable.Rows[Row];
// remove following check if you want to return "" instead of null
// but then you won't know if it was an empty or an undefined value
if (row.IsNull(Column))
return null;
return row[Column].ToString();
}
您还可以使用也支持可空类型的类型化Field
扩展方法,但由于您想对所有字段使用此方法,因此最好使用Object.ToString
如上所示。