/ Home / HowTo / AddGridDataRows

How to Configure DataAccess with ConnectionBuilder.
How to Configure DataAccess with DataConfigs.

The LJCDataGrid can be populated with DataTable Rows. It uses a helper class LJCGridDataLib.TableData.

Load Grid Rows from Table

using LJCDataAccess;
using LJCGridDataLib;
using LJCNetCommon;
using LJCWinFormControls;

internal void DataRetrieve(DataAccess dataAccess, LJCDataGrid ljcGrid)
{
  // Configure GridColumns.
  DataTable dataTable;
  DbColumns gridColumns = null;
  string sql;

  // *** Test Setting ***
  //var configType = "Manually";
  var configType = "FromTableColumns";
  switch (configType)
  {
    case "Manually":
      // Create Grid Columns manually.
      gridColumns = new DbColumns();
      gridColumns.Add("Name", maxLength: 60);
      gridColumns.Add("Description", maxLength: 100);
      gridColumns.Add("Abbreviation", maxLength: 15);
      break;

    case "FromTableColumns":
      // Create Grid Columns from DataTable.Columns.
      sql = "select * from Province";
      // Gets columns in "Select" column order.
      // If "*" gets columns in database order.
      dataTable = dataAccess.GetSchemaOnly(sql);
      var baseDefinition = TableData.GetDbColumns(dataTable.Columns);
      var propertyNames = new List()
      {
        { "Name" },
        { "Description" },
        { "Abbreviation" }
      };
      // Gets grid column definitions in propertyNames order.
      // If propertyNames is null, gets definitions in baseDefinition order.
      gridColumns = baseDefinition.LJCGetColumns(propertyNames);
      break;
  }
  // Sets grid column names to DbColumn PropertyNames in grid columns order.
  ljcGrid.LJCAddColumns(gridColumns);

  // Load the data.
  // The DataAccess class requires the developer
  // to create the SQL statements to pass to its methods.
  sql = "select * from Province";
  dataTable = dataAccess.GetDataTable(sql);
  if (NetCommon.HasData(dataTable))
  {
    // Create and populate the grid rows individually.
    foreach (DataRow dataRow in dataTable.Rows)
    {
      var ljcGridRow = ljcGrid.LJCRowAdd();
      // Adds the values in grid columns order.
      TableData.RowSetValues(ljcGridRow, dataRow);
    }
  }
}
/ Home / HowTo / AddGridDataRows

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.