/ Home / HowTo / DataGridView

This example uses the DataAccess code.


Adding Data to a DataGridView

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using PersonDAL;

namespace PersonApp
{
  public partial class PersonList : Form
  {
    #region Constructors

    /// <summary>Initializes an object instance.</summary<
    public PersonList()
    {
      InitializeComponent();
    }
    #endregion

    #region Form Event Handlers

    // Configures the form and loads the initial control data.
    private void PersonList_Load(object sender, EventArgs e)
    {
      InitializeControls();
      DataRetrieve();
    }
    #endregion

    #region Data Methods

    // Retrieves the list rows into a DataGridView Grid.
    private void DataRetrieve()
    {
      // Get the data.
      DataTable dataTable = mPersonManager.GetDataTable();

      // Add the grid rows.
      foreach (DataRow dataRow in dataTable.Rows)
      {
        // Add a new Grid Row.
        int index = PersonGrid.Rows.Add();
        DataGridViewRow gridRow = PersonGrid.Rows[index];
        gridRow.Height = 18;

        // Add the Data Values to the Grid Row.
        foreach (DataColumn dataColumn in dataTable.Columns)
        {
          string columnName = dataColumn.ColumnName;

          // Get the Data Value and create the Cell value.
          object cellValue = "";
          object dataValue = dataRow[columnName];
          if (dataValue != null)
          {
            cellValue = dataValue.ToString();
          }

          // The DataColumn name must be the same as the Grid Column name.
          if (mPersonDisplayNames.Contains(columnName))
          {
            // Set the Cell value.
            DataGridViewCell cell = gridRow.Cells[columnName];
            cell.Value = cellValue.ToString();
          }
        }
      }
    }
    #endregion

    #region Setup Methods

    // Configures the controls and loads the selection control data.
    private void InitializeControls()
    {
      // Initialize Class Data.
      mPersonManager = new PersonManager();

      // Set initial control values.
      SetupGridPerson();
    }

    // Setup the grid display columns.
    private void SetupGridPerson()
    {
      DataColumnCollection columns;

      // Specify the Grid Display Column names.
      mPersonDisplayNames = new string[]
      {
        "Name",
        "PrincipleFlag"
      };
      columns = mPersonManager.GetColumns(mPersonDisplayNames);

      PersonGrid.RowHeadersVisible = false;
      foreach (DataColumn dataColumn in columns)
      {
        // Set Grid Column name the same as the DataColumn name.
        string name = dataColumn.ColumnName;
        int index = PersonGrid.Columns.Add(name, name);
        DataGridViewColumn gridColumn = PersonGrid.Columns[index];
        gridColumn.Width = 150;
        if (gridColumn.Name.Equals("PrincipleFlag"
          , StringComparison.CurrentCultureIgnoreCase))
        {
          gridColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
      }
    }
    #endregion

    #region Class Data

    private PersonManager mPersonManager;
    private string[] mPersonDisplayNames;
    #endregion
  }
}

/ Home / HowTo / DataGridView

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