DataTableSource.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Data;
  2. using System.Linq;
  3. namespace Terminal.Gui {
  4. /// <summary>
  5. /// <see cref="ITableSource"/> implementation that wraps
  6. /// a <see cref="System.Data.DataTable"/>. This class is
  7. /// mutable: changes are permitted to the wrapped <see cref="System.Data.DataTable"/>.
  8. /// </summary>
  9. public class DataTableSource : ITableSource
  10. {
  11. /// <summary>
  12. /// The data table this source wraps.
  13. /// </summary>
  14. public DataTable DataTable { get; private set; }
  15. /// <summary>
  16. /// Creates a new instance based on the data in <paramref name="table"/>.
  17. /// </summary>
  18. /// <param name="table"></param>
  19. public DataTableSource(DataTable table)
  20. {
  21. this.DataTable = table;
  22. }
  23. /// <inheritdoc/>
  24. public object this [int row, int col] => DataTable.Rows[row][col];
  25. /// <inheritdoc/>
  26. public int Rows => DataTable.Rows.Count;
  27. /// <inheritdoc/>
  28. public int Columns => DataTable.Columns.Count;
  29. /// <inheritdoc/>
  30. public string [] ColumnNames => DataTable.Columns.Cast<DataColumn>().Select (c => c.ColumnName).ToArray ();
  31. }
  32. }