New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Binding to DataTable or DataSet
When not using declarative data sources, the most common means of providing a data source for a RadGrid control is probably using a DataTable or DataSet control. You can populate DataTable instance (part of DataSet object or not) with data from a source of your choice (database, custom object collection, xml file, etc.) and then pass it to the DataSource property of the control. Here is a sample that uses the NeedDataSource event, extracting the data from an SQL database:
ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CellSpacing="0"
GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1" AllowPaging="true"
PageSize="10">
<MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
</MasterTableView>
</telerik:RadGrid>
And in the code-behind:
C#
protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}
public DataTable GetDataTable(string query)
{
String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable myDataTable = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnString))
{
adapter.SelectCommand = new SqlCommand(query, conn);
adapter.Fill(myDataTable);
}
return myDataTable;
}