0

将特定 [Column][Row] 值放入 DataTable 的最佳方法是什么?

    private DataTable CurrentTable { get; set; }
    public string selectCell(int Column, int Row)
    {
        return CurrentTable............
    }
4

1 回答 1

1

使用适当的索引器:

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如上所示。

于 2013-10-29T12:18:16.037 回答