APNSoft Controls [4.5.2.20]
DataGrid > Binding to Data


APNSoft DataGrid displays data obtained from a database or other data source. The data is presented in tabular format. To bind to a data source, set the DataSource property and call the DataBind() method. DataGrid supports different data sources: DataView, DataTable, DataSet, DataReader and any object that implements IEnumerable interface.


[C#]
protected void Page_Load(object sender, EventArgs e)
{
    //Define SQL query
    string SqlQuery = @"SELECT CustomerID, CompanyName, ContactName FROM Customers";

    //Get DataTable
    DataTable myDataSource = GetDataTable(SqlQuery);

    //Set the data source
    myDataGrid.KeyFieldName = "CustomerID";
    myDataGrid.DataSource = myDataSource;
    myDataGrid.DataBind();
}
        

[VB.NET]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    'Define SQL query
    Dim SqlQuery As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers"

    'Get DataTable (MS Access Database)
    Dim myDataSource As DataTable = GetDataTable(SqlQuery)

    'Set the data source
    myDataGrid.KeyFieldName = "CustomerID"
    myDataGrid.DataSource = myDataSource
    myDataGrid.DataBind()

End Sub
        


Also, APNSoft DataGrid has the DataSourceID property. This property can be used to refer to actual .NET data source object (SqlDataSource) from which the DataGrid retrieves its list of data items.


[ASPX]
<asp:SqlDataSource id="mySqlDataSource" runat="server" 
    DataSourceMode="DataReader" 
    ConnectionString="..." 
    SelectCommand="..." />

<APNSoft:APNSoftDataGrid id="myDataGrid" runat="server" 
    DataSourceID="mySqlDataSource" />