DataTableSource.cs 938 B

1234567891011121314151617181920212223242526272829303132333435
  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="DataTable"/>.
  8. /// </summary>
  9. public class DataTableSource : ITableSource
  10. {
  11. private readonly DataTable table;
  12. /// <summary>
  13. /// Creates a new instance based on the data in <paramref name="table"/>.
  14. /// </summary>
  15. /// <param name="table"></param>
  16. public DataTableSource(DataTable table)
  17. {
  18. this.table = table;
  19. }
  20. /// <inheritdoc/>
  21. public object this [int row, int col] => table.Rows[row][col];
  22. /// <inheritdoc/>
  23. public int Rows => table.Rows.Count;
  24. /// <inheritdoc/>
  25. public int Columns => table.Columns.Count;
  26. /// <inheritdoc/>
  27. public string [] ColumnNames => table.Columns.Cast<DataColumn>().Select (c => c.ColumnName).ToArray ();
  28. }
  29. }