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);
}
}