选择DataTable作为单元格

时间:2013-10-29 12:14:16

标签: c# .net-3.5 system.data

将特定[Column] [Row]值转换为DataTable的最佳方法是什么?

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

1 个答案:

答案 0 :(得分: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 extension method,但由于您希望对所有字段使用此方法,因此最好使用Object.ToString,如上所示。