What is the best way to get a specific [Column][Row] value into a DataTable ?

    private DataTable CurrentTable { get; set; }
    public string selectCell(int Column, int Row)
    {
        return CurrentTable............
    }
有帮助吗?

解决方案

Use the appropriate indexer:

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();
}

You could also use the typed Field extension method which also supports nullable types, but since you want to use this method for all fields it's better to use Object.ToString as shown above.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top