سؤال

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