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

[Solved] WHY REBIND for SORT?

4 Answers 298 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 25 Jun 2008, 04:10 PM
Ok,  I am looking at all the examples and not seeing how I can maintain the sort with my grid.  I use an obejct hierarchy not some DataSourceID with SQL in the page :(

<telerik:RadGrid ID="RadGrid1" 
AllowMultiRowSelection="True"   
AllowSorting="True"   
AllowPaging="False"   
Skin="Vista"   
Width="95%"   
ShowStatusBar="true" 
AutoGenerateColumns="False"                     
GroupingEnabled="true" 
ShowHeader="true"                                   
GridLines="Horizontal"   
AlternatingItemStyle-BackColor="#ffffff" 
OnSortCommand="RadGrid1_SortCommand" 
runat="server"

<

Columns>

<telerik:GridBoundColumn SortExpression="ROWNUM" HeaderText="Priority" HeaderButtonType="TextButton" HeaderStyle-Width="50" ItemStyle-Width="50" DataField="ROWNUM" SortAscImageUrl="~/images/desc.gif" SortDescImageUrl="~/images/asc.gif"/>

</Columns>

  private void BindGrid()  
    {  
        CustomerListBLL customerList = new CustomerListBLL();  
        DataTable dtCustomers = new DataTable();  
        dtPricingQueue = customerList.GetCustomers;  
        RadGrid1.DataSource = dtCustomers ;  
        RadGrid1.DataBind();  

How does this rebind the data? I thought the Grid held the data in View State?  I DO NOT want to REBIND the data, the sort should be done on the client side.
 

  protected void RadGrid1_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)  
    {         
        if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))  
        {  
            GridSortExpression sortExpr = new GridSortExpression();  
            sortExpr.FieldName = e.SortExpression;  
            sortExpr.SortOrder = GridSortOrder.Ascending;  
 
            e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);                  
        }  
        //BindGrid();  I DO NOT WANT TO DO THIS
    }            

How do I get the sort to work on the CLIENT side with the data already in the GRID?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 26 Jun 2008, 06:55 AM
Hi Mike,

Try bind the Grid in the NeedDataSource event. Grid features like Sorting,Filtering etc work only with AdvanceDataBinding techniques. Go through the following demo link for more details.
Advanced data-binding

Princy.
0
Mike
Top achievements
Rank 1
answered on 26 Jun 2008, 12:32 PM
Thanks for your response, This is what I have.

  protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
    {  
        string gridSortString = this.RadGrid1.MasterTableView.SortExpressions.GetSortString();  
        string text = "Grid sort expression: " + gridSortString;  
        RadAjaxManager1.ResponseScripts.Add(string.Format("document.getElementById('{0}').innerHTML = '" + text + "';", this.SortLabel.ClientID));  
 
        DataSourceSelectArguments args = new DataSourceSelectArguments(gridSortString);  
        RadGrid1.DataSource = ViewState["DataTableCustomers"];  
    } 

As you notice I have had to rebind to ViewState which I set in my BindGrid()
CustomerListBLL customerList = new CustomerListBLL();  
DataTable dtCustomersnew DataTable();  
customerList.DepartmentNumberdepartmentNumber;  
 
customerList.EndDate = beginDate;  // passed by Button_Search
customerList.Status = status;  // passed by Button_Search
dtPricingQueue = customerList.GetCustomers;  
 
RadGrid1.DataSource = dtCustomers;  
RadGrid1.DataBind();  
 
//Throw the results into view state for sorting for RadGrid :(  
ViewState["DataTablePricingQueue"] = dtCustomers; 

BindGrid is kicked off from a Search Event:
    protected void ButtonSearch_Click(Object obj, EventArgs e)  
    {  
      BindGrid();  
    } 

I would like to get rid of the ViewState now you see what I have, your suggestions would be helpful.

ALSO, THIS SORT IS ONLY WORKING FOR DESCENDING.  It is your sample code and when I sort on a column, the down arrow shows up and the ASC is not populating the sort label as if the argument is not even sent.
    protected void RadGrid1_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)  
    {  
        if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))  
        {  
            GridSortExpression sortExpr = new GridSortExpression();  
            sortExpr.FieldName = e.SortExpression;  
            sortExpr.SortOrder = GridSortOrder.Ascending;    
            e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);  
        }  
    } 

I WILL NEED THESE SOLVED
0
Accepted
Shinu
Top achievements
Rank 2
answered on 27 Jun 2008, 08:08 AM
Hi Mike,
 
Try the doing Rebind() instead of DataBind() as shown below.

CustomerListBLL customerList = new CustomerListBLL();   
DataTable dtCustomersnew DataTable();   
customerList.DepartmentNumberdepartmentNumber;   
  
customerList.EndDate = beginDate;  // passed by Button_Search 
customerList.Status = status;  // passed by Button_Search 
dtPricingQueue = customerList.GetCustomers;   
  
//RadGrid1.DataSource = dtCustomers;   
//RadGrid1.DataBind();   
 
  
//Throw the results into view state for sorting for RadGrid :(   
ViewState["DataTablePricingQueue"] = dtCustomers;  
 
//Rebind will automatically call the NeedDataSource Event
RadGrid1.Rebind();   


Thanks
Shinu.
0
Mike
Top achievements
Rank 1
answered on 27 Jun 2008, 12:42 PM
Shinu,
Thanks for the response, that helps!
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or