This is probably the most commonly used means to generate content for Telerik RadGrid. 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 usage through the NeedDataSource event handler of Telerik RadGrid (the data is extracted from Access database):
| ASPX/ASCX |
Copy Code |
|
<rad:RadGrid id="RadGrid1" runat="server"> <MasterTableView AutoGenerateColumns="True" /> </rad:RadGrid> |
And in the code-behind:
| C# |
Copy Code |
|
private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e) { OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath ("~/Grid/Data/Access/Nwind.mdb")); OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT TOP 10 CustomerID, CompanyName, ContactName FROM Customers", MyOleDbConnection);
DataTable myDataTable = new DataTable();
MyOleDbConnection.Open(); try { MyOleDbDataAdapter.Fill(myDataTable); } finally { MyOleDbConnection. Close(); }
RadGrid1.DataSource = myDataTable.DefaultView;
} |
| VB.NET |
Copy Code |
|
Private Sub RadGrid1_NeedDataSource(ByVal [source] As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath ( "~/Grid/Data/Access/Nwind.mdb")) Dim MyOleDbDataAdapter As New OleDbDataAdapter MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT TOP 10 CustomerID, CompanyName, ContactName FROM Customers", MyOleDbConnection)
Dim myDataTable As DataTable = New DataTable()
MyOleDbConnection. Open() Try MyOleDbDataAdapter.Fill(myDataTable) Finally MyOleDbConnection. Close() End Try
RadGrid1.DataSource = myDataTable.DefaultView
End Sub |