Table of Contents
Populating the Grid

A Grid is almost always populated from a data source. To populate from a data source, you should set the Grid's DataSource and then call DataBind.

EO.Web supports the following type of data source object through DataSource property:

Data Source Remarks
DataView One GridColumn is created based on each row. Each GridColumn's DataField are used to evaluate values for each cell.
DataTable The DefaultView of the DataTable is actually used to populate the grid.
IDataReader Similar to DataView, each row is used to create a GridItem.
DataSet Grid.DataTable property is used locate the DataTable object that is to be used for populating the grid.
IEnumerable Each element of the enumerable will be used to create a GridItem. DataField is used as property name in this case. For example, the IEnumerable object can be an array of Point structure, while the Grid contains two columns with their DataField set to "X" and "Y" respectively.
The following code demonstrated how to populate the grid from an IDataReader object:

private void Populate()
{
    using (DemoDB db = new DemoDB())
    {    
        string sql = "SELECT * FROM Topics";
        Grid1.DataSource = db.ExecuteReader(sql);
        Grid1.DataBind();
    }
}

The above code populates the Grid based on the entire source data, which can be unworkable if there are too many records. In that case, you should enable paging. When paging is enabled, you will need to first set the total number of records, then populate only the current page.

private void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SetRecordCount();
        Populate();
    }
}

The following code demonstrates how to set the total number of records through RecordCount property:

private void SetRecordCount()
{
    using (DemoDB db = new DemoDB())
    {    
        string sql = "SELECT COUNT(*) FROM Topics";

        int nRecordCount = (int)db.ExecuteScalar(sql);
        Grid1.RecordCount = nRecordCount;
    }
}

The following code demonstrates how to populate the current page:

private void Populate()
{
    using (DemoDB db = new DemoDB())
    {    
        string sql1 = string.Format(
            "SELECT TOP {0} * FROM Topics ORDER BY PostedBy ASC", 
            (Grid1.CurrentPage + 1) * Grid1.PageSize);
    
        string sql2 = string.Format(
            "SELECT TOP {0} * FROM ({1}) ORDER BY PostedBy DESC",
            Grid1.PageSize, sql1);
    
        OleDbDataReader reader = db.ExecuteReader(sql2);
        Grid1.DataSource = reader;
        Grid1.DataBind();
    }
}