New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Binding To DataReader

You can extract values from your data source using a DataReader instance (calling the ExecuteReader() method for your OleDbCommand/SqlCommand command). That DataReader can be used as a RadGrid data source.

The following example shows how to use a DataReader using the grid's NeedDataSource event. It uses an Access data source and SqlDataReader:

RadGrid's declaration:

C#
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
    GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1" PageSize="10">
    <MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
    </MasterTableView>
</telerik:RadGrid>

Code-behind:

C#
SqlDataReader reader;
SqlConnection conn;

protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = ReadRecords("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}

private SqlDataReader ReadRecords(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    conn = new SqlConnection(ConnString);
    conn.Open();

    SqlCommand cmd = new SqlCommand(query, conn);
    reader = cmd.ExecuteReader();

    return reader;
}

protected void RadGrid1_DataBound(object sender, System.EventArgs e)
{
    reader.Close();
    conn.Close();
}

The most appropriate place to close the DataReader and the connection is in the DataBound event handler of the grid, as shown above.

Not finding the help you need?
Contact Support