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

RadGrid Sorting (SortCommand is not working)

3 Answers 363 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mahesh
Top achievements
Rank 1
mahesh asked on 26 Nov 2008, 10:55 AM
HI,
    I am having RadGrid where column of RadGrid created dynamically(through programing in NeedDataDource Event). Also "Allow Sorting" of  RadGrid is True. I also set true to "Allow Sorting" of each "GridBoundColumn" during creation in code behaind.
I am also using "needdatasource" event to set datasource of grid. But SortCommand event is not firing when i click on the column header.

Can you please let me know if these is any constrain.

3 Answers, 1 is accepted

Sort by
0
mahesh
Top achievements
Rank 1
answered on 27 Nov 2008, 06:59 AM
is there any one who is can put light on the above problem....

0
mahesh
Top achievements
Rank 1
answered on 28 Nov 2008, 09:12 AM
{
        RadGrid1.MasterTableView.Columns.Clear();
        GridBoundColumn objGridBoundColumn;

        objGridBoundColumn = new GridBoundColumn();
        objGridBoundColumn.HeaderText = "First";
        objGridBoundColumn.DataField = "First";
        objGridBoundColumn.UniqueName = "first";
        RadGrid1.MasterTableView.Columns.Add(objGridBoundColumn);

        objGridBoundColumn = new GridBoundColumn();
        objGridBoundColumn.HeaderText = "Second";
        objGridBoundColumn.DataField = "Second";
        objGridBoundColumn.UniqueName = "second";
        RadGrid1.MasterTableView.Columns.Add(objGridBoundColumn);
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        //Create column
       

        List<Demo> lstDemo;
        
        //Create datasource
        if ( Session["source"] == null )
        {
            lstDemo = new List<Demo>();
            for ( int i = 0; i < 1000; i++ )
            {
                Demo d = new Demo();
                d.First = i;
                d.Second = "Second" + i;
                lstDemo.Add(d);
            }

        }
        else
        {
            lstDemo = Session["source"] as List<Demo>;
            
        }

        if ( lstDemo != null )
        {
            RadGrid1.DataSource = lstDemo;
        }

    }
    protected void RadGrid1_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        int i = 0;
        //sorting expression
    }
}
public class Demo
{
    private int _First;
    public int First
    {
        get { return _First; }
        set { _First = value; }
    }

    private string _Second;
    public string Second
    {
        get { return _Second; }
        set { _Second = value; }
    }




<form id="form1" runat="server">
    <div>
      <asp:ScriptManager ID="ScriptManager1" runat="server">
      </asp:ScriptManager>
    
    </div>
      <telerik:RadGrid ID="RadGrid1"
      AllowPaging="false" ShowStatusBar="false"
      AutoGenerateColumns="false"
      AllowMultiRowSelection="true"
      AllowSorting="true"
      Height="97%" BorderWidth="0"
      Skin="Office2007"
      Width="99%"
      runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnSortCommand="RadGrid1_SortCommand">
          <MasterTableView ItemStyle-Wrap="true" HeaderStyle-Wrap="true" Width="100%" AllowSorting="true"
                TableLayout="Fixed" Height="100%" AutoGenerateColumns="false" AllowPaging="true"
                ShowFooter="false" AllowCustomPaging="True">
          </MasterTableView>
          <ItemStyle Wrap="false" />
        <PagerStyle NextPageText="Next" PrevPageText="Prev" Position="Top" Mode="NextPrevAndNumeric" />
      </telerik:RadGrid>
    </form>
0
Iana Tsolova
Telerik team
answered on 28 Nov 2008, 02:05 PM
Hello mahesh,

Please review this help topic on RadGrid programmatic creation. Check if you have created your grid following the steps described in the article. Note the differences in RadGrid dynamic creation on Page.Load and Page.Init:

Page.Load
RadGrid is statically declared and only its structure is defined in the Page_Load event handler. Here columns are first added to the MasterTableView columns collection and then their properties are set.

Page.Init
RadGrid is entirely created in the Page_Init event handler. Columns properties are set first and then the column is added to the columns collection of the MasterTableView.

Let me know how it goes.

Best wishes,
Iana
the Telerik team

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