 |
Simple data-binding through the DataBind() method can be used in simple scenarios which does not require complex operations like insert/delete/update through custom edit forms (WebUserControl and FormTemplate), grouping, hierarchy relations, etc. For advanced features like grouping, hierarchy presentation, filtering. etc. Telerik RadGrid requires advanced data-binding through its NeedDataSource event or DataSource control (under .NET 2.0). The NeedDataSource event is especially designed to facilitate the work of the developers as you do not need to handle any sorting/paging/grouping/filtering/etc. commands manually. Under such circumstances Telerik RadGrid will be "smart enough" to perform the corresponding operation automatically. |
1. Set the DataSource property. This property points the database which will be used as a source to Telerik RadGrid.
2. Call the DataBind() method when appropriate.
The correct approach when using simple data-binding is to call the DataBind() method on the first page load (after postback Telerik RadGrid uses the viewstate to recreate the data) and after handling some event (sort event for example). Keep in mind that if you choose simple data-binding, you will need to assign data-source and rebind the grid after each operation (paging, sorting, editing, etc.) - this copies exactly MS DataGrid behavior.
| C# |
Copy Code |
|
private void LoadData() { OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Data/Access/NWind.mdb")); OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
DataSet MyDataSet = new DataSet();
MyOleDbConnection.Open(); try { MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM Customers", MyOleDbConnection); MyOleDbDataAdapter.Fill(MyDataSet, "Customers"); } finally { MyOleDbConnection.Close(); }
DataView myDataView = MyDataSet.Tables["Customers"].DefaultView;
RadGrid1.DataSource = myDataView; } ...
private void InitializeComponent() { this.RadGrid1.PageIndexChanged += new Telerik.WebControls.GridPageChangedEventHandler(this.RadGrid1_PageIndexChanged); this.Load += new System.EventHandler(this.Page_Load); } ...
private void RadGrid1_PageIndexChanged(object source, Telerik.WebControls.GridPageChangedEventArgs e) { this.RadGrid1.CurrentPageIndex = e.NewPageIndex; LoadData(); this.RadGrid1.DataBind(); } |
| VB.NET |
Copy Code |
|
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load If Not Me.IsPostBack Then LoadData() RadGrid1.DataBind() End If End Sub
... Private Sub LoadData() Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Data/Access/NWind.mdb")) Dim MyOleDbDataAdapter As New OleDbDataAdapter()
Dim MyDataSet As New DataSet()
MyOleDbConnection. Open() Try MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT * FROM Customers", MyOleDbConnection) MyOleDbDataAdapter.Fill(MyDataSet, "Customers") Finally MyOleDbConnection. Close() End Try
Dim myDataView As DataView = MyDataSet.Tables("Customers").DefaultView
RadGrid1.DataSource = myDataView End Sub
... Private Sub RadGrid1_PageIndexChanged([source] As Object, e As Telerik.WebControls.GridPageChangedEventArgs) Handles RadGrid1.PageIndexChanged Me.RadGrid1.CurrentPageIndex = e.NewPageIndex LoadData() Me.RadGrid1.DataBind() End Sub |
See Also