RadGrid for ASP.NET

Advanced Databinding (using NeedDataSource event) Send comments on this topic.
Populating the control with data > Understanding databinding > Advanced Databinding (using NeedDataSource event)

Glossary Item Box

Telerik RadGrid fires this event each time it needs to be bound to a data source. 

The key to the advanced data binding of Telerik RadGrid is handling correctly the NeedDataSource event. That's why we chose this exact name. 'Need' in this case actually means that if at an exact moment the data-source property does not point to a valid data-source object, the grid will not behave correctly. This event fires in the following cases:

  • Right after OnLoad - if the grid has not been data-bound yet and there is no ViewState data. This also means that if MasterTableView.DataSourcePersistenceMode has been set to NoPersistence, grid would bind each time the page loads (not only the first time);
  • When paging operation occurs;
  • When sorting operation occurs;
  • When Edit command is fired;
  • Right after Update/Delete/Insert command event handlers finish execution. You can cancel these operations handling ItemCommand event and assigning false value to the e event argument's Canceled property;
  • When grouping / ungrouping - right after the RadGrid.GroupsChanging event is raised;
  • When filtering (choosing an option from a column filter menu);
  • When resorting a group;
  • When a call to the Rebind() grid method takes place;
  • Prior to any detail table binding;
  • In some other custom cases.
Important: You should never call Rebind() method in NeedDataSource event handler. You should never call DataBind() as well when using advanced data-binding through NeedDataSource. For MS GridView-like data-binding see the Simple data-binding topic in the same chapter.

Note that as of version 2.0, Telerik RadGrid will not fire NeedDataSource unless it is visible (Visible=true).
In version 2.0 we introduced also a variable in the event argument that can be used to determine the reason of firing the event.

The advantages of using this event are that the developer does not need to write any code handling the logic about when and how the data-binding should take place. It is still the developer's responsibility to properly construct a data source object and assign it to the RadGrid's DataSource property.

In the code of this event you should prepare the data source (list of objects) for Telerik RadGrid and assign it to the grid's DataSource property.

 

 

VB.NET Copy Code
Private Sub RadGrid1_NeedDataSource(ByVal [source] As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs)
          Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("Nwind.mdb"))
          Dim MyOleDbDataAdapter As New OleDbDataAdapter
         MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT * FROM Customers", MyOleDbConnection)
          Dim myDataTable As New DataTable
         MyOleDbConnection. Open()
         Try
              MyOleDbDataAdapter.Fill(myDataTable)
          Finally
              MyOleDbConnection. Close()
           End Try
          RadGrid1.DataSource = myDataTable
End Sub
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("Nwind.mdb"));
         OleDbDataAdapter MyOleDbDataAdapter =
new OleDbDataAdapter();
         MyOleDbDataAdapter.SelectCommand =
new OleDbCommand("SELECT * FROM Customers", MyOleDbConnection);
         DataTable myDataTable =
new DataTable();
         MyOleDbConnection. Open();
         
try
        {
            
MyOleDbDataAdapter.Fill(myDataTable);
         }
         
finally
        {
            
MyOleDbConnection. Close();
        }

       RadGrid1.DataSource = myDataTable;
}