This is a migrated thread and some comments may be shown as answers.

How do i add sorting and pagination to a dynamically created RadGrid

3 Answers 447 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 06 Mar 2017, 07:45 PM

I have manually created RadGrids based on data.  The grids create just fine but I don't know how to add the sorting and pagination to a DYNAMICALLY created RadGrid.  

Here is my RadGrid creation code:

Private Function CreateGrid(ByVal strLine As String) As RadGrid         

        Dim rg As New RadGrid         

      rg.ID = strLine         rg.Width = Unit.Percentage(280)         rg.PageSize = 40         rg.AllowPaging = True         rg.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric         rg.PagerStyle.AlwaysVisible = True         rg.MasterTableView.PagerStyle.AlwaysVisible = True         rg.AutoGenerateColumns = False         rg.ExportSettings.FileName = strLine         Dim boundColumn As GridBoundColumn         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Process"         boundColumn.HeaderText = "Process"         boundColumn.UniqueName = "Process"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Product"         boundColumn.HeaderText = "Product"         boundColumn.UniqueName = "Product"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Tested"         boundColumn.HeaderText = "Tested"         boundColumn.UniqueName = "Tested"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Passed"         boundColumn.HeaderText = "Passed"         boundColumn.UniqueName = "Passed"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Failed"         boundColumn.HeaderText = "Failed"         boundColumn.UniqueName = "Failed"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Yield"         boundColumn.HeaderText = "Yield"         boundColumn.UniqueName = "Yield"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "Top5"         boundColumn.HeaderText = "Top 5 Rejects"         boundColumn.UniqueName = "Top5"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left         boundColumn = New GridBoundColumn         rg.MasterTableView.Columns.Add(boundColumn)         boundColumn.DataField = "TrueFailure"         boundColumn.HeaderText = "True Failure?"         boundColumn.UniqueName = "TrueFailure"         boundColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center         boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center         Return rg     

End Function

The way I have done it on a project that was NOT dynamically created:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load         

    If Not Page.IsPostBack Then             

        Session("Report") = CreateTable()             

    End If     

End Sub

 

Protected Sub rdgAgeReport_PageIndexChanged(sender As Object, e As Telerik.Web.UI.GridPageChangedEventArgs) Handles rdgAgeReport.PageIndexChanged         

    rdgAgeReport.DataSource = CType(Session("Report"), DataTable)        

    rdgAgeReport.DataBind()     

End Sub Protected Sub rdgAgeReport_PageSizeChanged(sender As Object, e As Telerik.Web.UI.GridPageSizeChangedEventArgs) Handles rdgAgeReport.PageSizeChanged         

    rdgAgeReport.DataSource = CType(Session("Report"), DataTable)         

    rdgAgeReport.DataBind()     

End Sub

 

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 09 Mar 2017, 06:48 AM
Hi Mark,

Please make sure that you are not using the DataBind() method to bind the grid. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:

Declarative DataSource (DataSourceID property)
Programmatic Data Binding (NeedDataSource event, + DetailTableDataBind for hierarchy). You should set the DataSource property ONLY within these event handler.

Also, it is a better idea to create the grid on Page_Init. In this case, the properties of the columns should be set before adding them to the MasterTableView.Columns collection:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-a-radgrid-on-page_init

If you will change the grid's structure depending on some user selection, please follow the steps demonstrated in the following article:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/changing-the-grid-structure-dynamically-on-postback

I hope this will prove helpful.

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 1
answered on 10 Mar 2017, 01:31 PM
I cannot do this at page init.  This app is basically a report.  The user selects how many lines and that's how many grids I create AFTER the page is loaded.  So now back to my original question of how to sort and paginate after I manually create multiple rad grids.  I was unable to find any code or suggestions.
0
Accepted
Marin Bratanov
Telerik team
answered on 15 Mar 2017, 09:14 AM

Hello Mark,

The key thing is that you must provide a grid data source in either of the following ways:

  • a declarative data source (like an SqlDataSource), via the DataSourceID string property
  • OR, set the DataSource property of the grid ONLY in its OnNeedDataSource event handler. You can attach the handler when creating the grid: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-a-radgrid-on-page_load. Here is a modified version of the C# example:

    protected void Page_Load(object sender, System.EventArgs e)
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        PlaceHolder1.Controls.Add(RadGrid1);
        RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
        //Add RadGrid to the Controls collection of the placeholder
        if (!IsPostBack)
        {
            RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
            RadGrid1.AllowPaging = true;
            RadGrid1.AllowFilteringByColumn = true;
            RadGrid1.MasterTableView.AutoGenerateColumns = false;
            RadGrid1.PageSize = 15;
            RadGrid1.AllowSorting = true;
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "CustomerID";
            boundColumn.HeaderText = "CustomerID";
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "ContactName";
            boundColumn.HeaderText = "Contact Name";
        }
    }
     
    void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetGridData();
    }
     
    protected DataTable GetGridData()
    {
        DataTable tbl = new DataTable();
        tbl.Columns.Add(new DataColumn("CustomerID", typeof(decimal)));
        tbl.Columns.Add(new DataColumn("ContactName", typeof(string)));
        tbl.Columns.Add(new DataColumn("aColumn", typeof(int)));
        tbl.Columns.Add(new DataColumn("someColumn", typeof(string)));
        tbl.Rows.Add(new object[] { 1, "one", 2, "5" });
        tbl.Rows.Add(new object[] { 2, "two", 3, null/*SIMULATE EMPTY VALUE*/ });
        tbl.Rows.Add(new object[] { 3, "three", 4, "5" });
        tbl.Rows.Add(new object[] { 4, "four", 5, "5" });
     
        return tbl;
    }

Regards,

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Mark
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or