Telerik
Home / Community / Forums / Grid / GridTemplateColumn Sorting

GridTemplateColumn Sorting

Feed from this thread
  • Alfred Pena Intermediate avatar

    Posted on Oct 9, 2007 (permalink)

    Hi.

    I having trouble sorting a Collection based on ArrayList. The problem doesn't happen if I use GridBoundColumn but if I use a GridTemplateColumn and click on the header column I get "Cannot Find Column" error. I have followed the procedure in your documentation and Examples with no luck. Here is a sample of my code:


    <radG:RadGrid ID="RadGrid1" runat="server"   
            AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"   
            GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" AllowPaging="True"  Skin="VOGrid"   
            AllowSorting="True" OnSortCommand="RadGrid1_SortCommand" MasterTableView-RetrieveAllDataFields="false">  
            <PagerStyle Mode="NextPrevAndNumeric" /> 
              <ExportSettings> 
                  <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in" 
                      PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in"  /> 
              </ExportSettings> 
              <MasterTableView > 
                  <Columns> 
                    <radG:GridTemplateColumn HeaderText="Company Name" UniqueName="TemplateColumn" Visible="False">  
                     <ItemTemplate> 
                         <asp:LinkButton ID="LB_OrganizationName" runat="server">LinkButton</asp:LinkButton> 
                     </ItemTemplate> 
                        <ItemStyle Width="50%" /> 
                    </radG:GridTemplateColumn> 
                     <radG:GridTemplateColumn HeaderText="Name" UniqueName="TemplateColumn1" DataType="System.String" SortExpression="Name">  
                     <ItemTemplate> 
                         <asp:LinkButton ID="LB_OrganizationAlias" Text=<%# Eval("Name") %> runat="server">LinkButton</asp:LinkButton> 
                     </ItemTemplate> 
                         <ItemStyle Width="50%" /> 
                    </radG:GridTemplateColumn> 
                     
                      
                  </Columns> 
                   
              </MasterTableView> 
            
          </radG:RadGrid> 



    protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)  
        {  
            LinkButton LB_OrganizationName;  
            LinkButton LB_OrganizationAlias;  
           
     
             if (e.Item is GridDataItem)  
             {  
                 GridDataItem dataItem = e.Item as GridDataItem;  
     
                 LB_OrganizationName = (LinkButton) dataItem.FindControl("LB_OrganizationName");  
                 objCompany = (Company) CompanyList[dataItem.DataSetIndex];  
                 LB_OrganizationName.Text = objCompany.Name;  
                 LB_OrganizationAlias = (LinkButton)dataItem.FindControl("LB_OrganizationAlias");  
               //  LB_OrganizationAlias.Text = objCompany.Name;  
                 LB_OrganizationName.Style.Add("text-decoration", "none");  
                 LB_OrganizationName.Attributes.Add("onmouseover", "tS=this.style;tS.textDecoration='underline';tS.color='blue';tS.fontWeight='bold';");  
                 LB_OrganizationName.Attributes.Add("onmouseout", "tS=this.style;tS.textDecoration='none';tS.fontWeight='normal'");  
                 LB_OrganizationAlias.Style.Add("text-decoration", "none");  
                 LB_OrganizationAlias.Attributes.Add("onmouseover", "tS=this.style;tS.textDecoration='underline';tS.color='blue';tS.fontWeight='bold';");  
                 LB_OrganizationAlias.Attributes.Add("onmouseout", "tS=this.style;tS.textDecoration='none';tS.fontWeight='normal'");  
                
                 
             }  
     
        }  
     
     
    protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)  
        {  
     
             
     
            qrOrg = OrgS.GetAll();  
     
            CompanyList = new ArrayList();  
            foreach (RdfDoOrganization Org in qrOrg)  
            {  
                objCompany = new Company();  
                objCompany.Name = Org.Name;  
                objCompany.Alias = Org.Name;  
     
                CompanyList.Add(objCompany);  
            }  
            
            RadGrid1.DataSource = CompanyList;  
              
     
        }  
     
     public class Company  
            {  
                string name;  
                string alias;  
     
                public string Name  
                {  
                    set { name = value; }  
                    get { return name; }  
                }  
     
                public string Alias  
                {  
                    set { alias = value; }  
                    get { return alias; }  
                }  
            }  
     
     



    Thank you for help,

    Alfred

    Reply

  • Princy MVP Princy's avatar

    Posted on Oct 9, 2007 (permalink)

    Hi Alfred,

    You have not added the SortExpression to the TemplateColumn with the Header Text as CompanyName:

    <radG:GridTemplateColumn HeaderText="Company Name" UniqueName="TemplateColumn" Visible="False"

    Princy

    Reply

  • Alfred Pena Intermediate avatar

    Posted on Oct 10, 2007 (permalink)

    Hi Princy,

    Thank you for your response. The Template Column that I want to sort on is the  the second template column called "Name". That Template has the sortexpression set to "Name".

    Regards,

    Richard

    Reply

  • Shinu MVP Shinu's avatar

    Posted on Oct 11, 2007 (permalink)

    Hi Alfred Pena,

    From the aspx design you provided I found that you didint set the DataField  property of GridTemplateColumn.
    Try setting the DataFiled property and let me know the problem still persist.

    <radG:GridTemplateColumn HeaderText="Name" UniqueName="TemplateColumn1" DataField="Name"  DataType="System.String" SortExpression="Name">  
                     <ItemTemplate> 
                         <asp:LinkButton ID="LB_OrganizationAlias" Text=<%# Eval("Name") %> runat="server">LinkButton</asp:LinkButton> 
                     </ItemTemplate> 
                         <ItemStyle Width="50%" /> 
                    </radG:GridTemplateColumn> 


    Shinu.

    Reply

  • Alfred Pena Intermediate avatar

    Posted on Oct 11, 2007 (permalink)

    Hi Shinu,

    The problem was that I had set the  RetrieveAllDataFields=false.
    I tried setting the DataField as you suggested but that did not work.
    Only after setting the RetrieveAllDataFields=true then it works
    I am not sure why would I need all columns when I have set the DataField="Name" for a template column. If I set RetrieveAllDataFields=false and only use bound columns the sorting works too but in the other hand if it is a template bould column it does not work.

    Any Ideas? keep in mind that the datasource is an ArrayList of objects.

    Thank you,

    Alfred

    Reply

  • Telerik Admin admin's avatar

    Posted on Oct 12, 2007 (permalink)

    Hi Alfred,

    Have you used the RetrieveAllDataFields = false in conjunction with the AdditionalDataFieldNames property of RadGrid? Here is a short description of these grid attributes:

    To prevent the grid from traversing all data fields in that collection you just have to set the property RadGridInstance.MasterTableView.RetrieveAllDataFields to false. In this case, the additional fields that you might use when sorting, grouping or used in DataKeyNames should be included in the AdditionalDataFieldNames array. With the settings specified in this paragraph, only the properties that are used as column DataFields or those specified in the AdditionalDataFieldNames will be extracted.

    For additional details review the topic from the online documentation pointed below:

    http://www.telerik.com/help/aspnet/grid/?grdTipsWhenBindingToCustomCollections.html

    Best regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center

    Reply

  • Alfred Pena Intermediate avatar

    Posted on Oct 13, 2007 (permalink)

    Thank you stephen.

    I missed that detail in the documentation.

    Regards,

    Alfred.

    Reply

Powered by Sitefinity ASP.NET CMS

Contact Us | Site Feedback | Terms of Use | Privacy Policy
Copyright © 2002-2010 Telerik. All rights reserved.