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

Dynamic grid creation, sorting and paging dont work together

3 Answers 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ameet
Top achievements
Rank 1
Ameet asked on 24 Sep 2008, 06:01 PM
 I want to create number of rad grids on a user control. This number is driven from DB.
As a sample I created a single Grid on ASPX page,
after reading all the related threads and help for dynamic creation.

Every thing works fine, including the sorting.
But when Sorting and Paging both are enabled then, we have issue.
It seems that the grid is sorted on last and first page click.

Below is the code.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.WebControls;
using ClickTactics.Data.ObjectMapping;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  //if (!IsPostBack)
  //{
  //    DefineGridStructure();
  //}
    }

 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
 {

  //if you allow the id of the header buttons to be autgenerated
  //for some reason they change on postback after sorting which then
  //fires an error after the 2nd attempt to sort...this is a workaround
  if (e.Item is GridHeaderItem)
  {
   foreach (TableCell cell in e.Item.Cells)
   {
    foreach (Control ctrl in cell.Controls)
    {
     if (ctrl is LinkButton)
     {
      ctrl.ID = "btn" + (ctrl as LinkButton).ClientID;

     }
    }
   }
  }
 }

 

 protected void Page_Init(object sender, System.EventArgs e)
 {
  DefineGridStructure();
 }
 private void DefineGridStructure()
 {
  
  RadGrid grid = new RadGrid();
  grid.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
  grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);

 

  grid.ID = "RadGrid1";// +DateTime.Now.ToString();
  grid.MasterTableView.ID = "RadGrid1MTView";
  
  grid.Skin = "Inox";
  grid.Width = Unit.Percentage(100);
  grid.PageSize = 1;
  grid.AllowPaging = true;
  grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
  grid.AutoGenerateColumns = false;
  grid.AllowSorting = true;
  

  //Add Customers table
  grid.MasterTableView.PageSize = 2;
  grid.MasterTableView.Width = Unit.Percentage(100);
  grid.MasterTableView.DataKeyNames = new string[] { "EmpID" };
  GridBoundColumn boundColumn = new GridBoundColumn();
  
  boundColumn.DataField = "EmpID";
  boundColumn.HeaderText = "Emp ID";
  grid.MasterTableView.Columns.Add(boundColumn);
  boundColumn = new GridBoundColumn();
  boundColumn.DataField = "EmpName";
  boundColumn.HeaderText = "EmpName";
  grid.MasterTableView.Columns.Add(boundColumn);

 

 

  grid.DataSource = GetDataTable();

  grid.DataBind();
  //Add Orders table
  

  // Add the grid to the placeholder
  this.PlaceHolder1.Controls.Add(grid);
 }

  
 }


3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 25 Sep 2008, 06:58 AM
Hello Ameet,

You should not call DataBind() on every page lifecycle. Please remove this! I'm not sure also about the code in ItemCreated - can you clarify?

Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ameet
Top achievements
Rank 1
answered on 25 Sep 2008, 09:49 AM
Thanks for the reply.

Well the code in itemcreated i had seen from another post on telerik.


So even if we are create the complete grid dynamically, the creation of grid code, should that be written in IsPostback = false?

Is there some sample code that has complete grid created dynamically and sorting and paging is enabled ?

Regards
Ameet Shah     
0
Sebastian
Telerik team
answered on 25 Sep 2008, 10:04 AM
Hi Ameet,

You may refer to the following online demo of RadGrid (under the 'Programmatic creation' section in the QSF):

http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/Programming/Hierarchy/DefaultCS.aspx

as well as the 'Programmatic creation' topic in the documentation:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

There are also running examples posted in the public forum where the grid is created dynamically on PageInit or PageLoad and its paging/sorting features are enabled - consider performing a quick search for them, too.
 
Best,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Ameet
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Ameet
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or