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

Creating the RadGrid Programmatically - Sorting not working

4 Answers 374 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gerard
Top achievements
Rank 1
Gerard asked on 23 May 2009, 09:17 PM

Hi,

I've got a page with one Placeholder on it and nothing else. I've added the RadScriptManager and the RadGrid using the code below and the Paging works ok but the sorting doesn't?  No error is thrown. It shows the sorting icons in the column heading but does not actually do the sorting.

If I do exaclty the same using the Designer pages it all works fine. It's trying to do things programmatically that I can't get it to work.

 

default.aspx
==========
<%@ Page EnableEventValidation="false"  Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TelerikGridExample._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<asp:PlaceHolder runat="server" ID="ph"></asp:PlaceHolder>
</body>
</html>

default.aspx.cs
==============
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TelerikGridExample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {

                HtmlForm f1 = new HtmlForm();

                // script manager
                Telerik.Web.UI.RadScriptManager sm = new Telerik.Web.UI.RadScriptManager();
                sm.ID = "RadScriptManager1";

                // data source
                SqlDataSource ds = new SqlDataSource(@"Data Source=gbe-pc\express2008;Initial Catalog=Northwind;User ID=sa;Password=something", "SELECT categoryid, categoryname FROM [categories]");
                ds.ID = "SqlDataSource1";

                // rad grid
                Telerik.Web.UI.RadGrid rg = new Telerik.Web.UI.RadGrid();
                rg.ID = "RadGrid1";
                rg.PageSize = 5;
                rg.AllowPaging = true;
                rg.AllowSorting = true;
                rg.AutoGenerateColumns = false;
                rg.GroupingEnabled = true;
                rg.ClientSettings.AllowDragToGroup = true;
                rg.EnableViewState = false;

                // add some columns
                Telerik.Web.UI.GridBoundColumn c1 = new Telerik.Web.UI.GridBoundColumn();
                rg.MasterTableView.Columns.Add(c1);
                c1.DataField = "categoryid";
                c1.SortExpression = "categoryid";
                c1.UniqueName = "categoryid";
                c1.DataType = System.Type.GetType("System.Int32");
                c1.HeaderText = "categoryid";

                Telerik.Web.UI.GridBoundColumn c2 = new Telerik.Web.UI.GridBoundColumn();
                rg.MasterTableView.Columns.Add(c2);
                c2.DataField = "categoryname";
                c2.SortExpression = "categoryname";
                c2.UniqueName = "categoryname";
                c2.DataType = System.Type.GetType("System.String");
                c2.HeaderText = "categoryname";
               
                rg.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(rg_NeedDataSource);
                // add to controls to the ajax panel
                f1.Controls.Add(sm);
                f1.Controls.Add(ds);
                f1.Controls.Add(rg);

                ph.Controls.Add(f1);     
        }

        void rg_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            Telerik.Web.UI.RadGrid myrg = (Telerik.Web.UI.RadGrid)Page.FindControl("RadGrid1");
            myrg.DataSource = (SqlDataSource)Page.FindControl("SqlDataSource1");

        }

    }
}

 

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 May 2009, 03:58 AM
Hi Gerad,

When generating a grid in the Page_Init event handler, grid columns should be added to the Controls collection of the MasterTableView after their attributes are set. No ViewState is required for grid structure to be persisted as it is recreated on each page initialization. Here in the above given code you are adding the column to the column collection first and then setting its properties. Go through the following help article for more details.
Creating the grid entirely in the code behind.


Shinu

0
Gerard
Top achievements
Rank 1
answered on 25 May 2009, 05:18 PM
Hi Shinu, Thanks very much for your reply. I have moved line

rg.MasterTableView.Columns.Add(c1);

To after setting the properties but this doesn’t appear to make any difference.

Thanks also for the link you sent I did notice that I was not setting the DataKetNames value but again this has not solved things.

rg.MasterTableView.DataKeyNames = new string[] { "categoryid" };

The BIG difference to my example and the ones give in the above link is that I am adding a HTMLForm dynamically and not using <form runat=”server”>. It’s seem very close to working (the grid appears ok, page moves work ok but does not the sorting) but am I trying to do something that is not feasible or is there something missing to do the persistence of the data.

Gerard
0
Surendra
Top achievements
Rank 1
answered on 05 Jun 2014, 06:30 AM
HI Gerard,

I am also facing the same problem. Did you find any solution yet. Please share with me if you have solved it already. I need to fix it urngently.

Thanks & Regards
Surendra Kumar
0
Shinu
Top achievements
Rank 2
answered on 05 Jun 2014, 06:49 AM
Hi Surendra,

Try setting the OverrideDataSourceControlSorting property of the grid to true and let me know if this helps. If not provide your full code snippet.

C#:
RadGrid1.MasterTableView.OverrideDataSourceControlSorting = true;

Thanks,
Shinu
Tags
Grid
Asked by
Gerard
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Gerard
Top achievements
Rank 1
Surendra
Top achievements
Rank 1
Share this question
or