Вопрос

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