Rad Grid column header text not appearing on dynamically generated grid after sort

15 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    134 posts
    Member since:
    Sep 2007

    Posted 18 Nov 2010 Link to this post

    Hi I dynamically generate the columns in a grid and then load and bind a datasource, everything looks good until I press the column header to sort the grid, the grid does not sort and the column headers disappear.  What Am I missing:

    The grid declaration:

    <telerik:RadGrid 
                                   ID="radGridOutput" 
                                   runat="server" 
                                   AllowPaging="True" 
                                   Width="100%"
                                   enableEmbeddedSkins="false"
                                   Skin="CorpsNET"
                                   AllowSorting="true"
                                   pagesize="10"  
                                   ClientSettings-AllowKeyboardNavigation="false"
                                   ItemStyle-HorizontalAlign="Center"
                                   HeaderStyle-HorizontalAlign="Center"
                                   AlternatingItemStyle-HorizontalAlign="Center" 
                                   onpageindexchanged="radGridOutput_PageIndexChanged" 
                                   onsortcommand="radGridOutput_SortCommand" 
                                   onitemdatabound="radGridOutput_ItemDataBound">
                                   <PagerStyle Mode="NextPrevAndNumeric" />
                                   <MasterTableView 
                                       AutoGenerateColumns="false" 
                                       Width="100%"
                                       CommandItemDisplay="None" 
                                       PageSize="10">
                                       <Columns>
                                       </Columns>
                                       <EditFormSettings>
                                           <PopUpSettings ScrollBars="None" />
                                       </EditFormSettings>
                                       <NoRecordsTemplate>
                                           <div align="center" style="color:darkorange">---------- No Records to Display -----------</div
                                       </NoRecordsTemplate>
                                   </MasterTableView>
    <ClientSettings>
                                    </ClientSettings>
                                </telerik:RadGrid>
    Clicking a button loads the grid the first time:

    protected void btnCreateReport_Click(object sender, EventArgs e)
    {
        LoadReport(true);
        RadTabStrip1.SelectedIndex = 1;
        RadMultiPage1.SelectedIndex = 1;
    }



    The Code:
       protected void LoadReport(bool firstLoad)
        {
            //if we are paging or sorting don't erase the columns
            if (firstLoad == true)
            {
                radGridOutput.DataSource = null;
                radGridOutput.MasterTableView.DataKeyNames = null;
                radGridOutput.MasterTableView.Columns.Clear();
                radGridOutput.Columns.Clear();
            }
      
                    if (firstLoad == true)
                    {
                        radGridOutput.MasterTableView.AutoGenerateColumns = false;
                        radGridOutput.AutoGenerateColumns = false;
      
                        //string[] _keys3 = { "ID" };
      
                        //radGridOutput.MasterTableView.DataKeyNames = _keys3;
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "RosterDate", DataField = "RosterDateString", HeaderText = "Date", SortExpression = "RosterDate" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "RosterDay", DataField = "RosterDay", HeaderText = "Day" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "SponsorNumberAndName", DataField = "SponsorNumberAndName", HeaderText = "Sponsor", SortExpression = "SponsorNumberAndName" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "LocationNumberAndName", DataField = "LocationNumberAndName", HeaderText = "Location", SortExpression = "LocationNumberAndName" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "CrewName", DataField = "CrewName", HeaderText = "Crew", SortExpression = "CrewName" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "SupervisorName", DataField = "SupervisorName", HeaderText = "Supervisor", SortExpression = "SupervisorName" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "NumberOfCrewmembers", DataField = "NumberOfCrewmembers", HeaderText = "# Crew Members", SortExpression = "NumberOfCrewmembers" });
                        radGridOutput.Columns.Add(new GridBoundColumn() { UniqueName = "TotalHours", DataField = "totalhours", HeaderText = "Total Hours", SortExpression = "totalHours" });
      
                    }
      
                    if (rdoPayPeriod.Checked == true)
                    {
                        PayPeriod _payPeriod = PayPeriodService.GetByID(Int32.Parse(ddlPayPeriod.SelectedValue.ToString()));
                        radGridOutput.DataSource = RptProjectHoursDetailService.GetReport(_payPeriod.StartDate, _payPeriod.EndDate, ddlSponsor.SelectedIndex>0?Int32.Parse(ddlSponsor.SelectedValue.ToString()):0);
                    }
                    else if (rdoSchoolMonth.Checked == true)
                    {
                        SchoolMonth _schoolMonth = SchoolMonthService.GetByID(Int32.Parse(ddlSchoolMonth.SelectedValue.ToString()));
                        radGridOutput.DataSource = RptProjectHoursDetailService.GetReport(_schoolMonth.StartDate, _schoolMonth.EndDate, ddlSponsor.SelectedIndex > 0 ? Int32.Parse(ddlSponsor.SelectedValue.ToString()) : 0);
                    }
                    else if (rdoSpecifiedTimePeriod.Checked == true)
                    {
                        radGridOutput.DataSource = RptProjectHoursDetailService.GetReport(DateTime.Parse(rdpStartDate.DbSelectedDate.ToString()), DateTime.Parse(rdpEndDate.DbSelectedDate.ToString()), ddlSponsor.SelectedIndex > 0 ? Int32.Parse(ddlSponsor.SelectedValue.ToString()) : 0);
                    }
                    radGridOutput.DataBind();
          }

    The ourput of the GetReport Call is a business object with properties that match the column names and here is the sort command procedure:

    protected void radGridOutput_SortCommand(object source, GridSortCommandEventArgs e)
      {
          LoadReport(false);
      }

    I can't see anything amiss that would cause this issue.  I'm currently not using any type of AJAX management on the page.

    Thanks in Advance

    Jonathan

  2. Marin
    Admin
    Marin avatar
    1043 posts

    Posted 24 Nov 2010 Link to this post

    Hi Jonathan,

    When using complex functionalities like sorting/filtering etc. you should use the Advanced Databinding for the RadGrid, making use of the need datasource event the grid is bound exactly when needed. This entails also that you remove the LoadReport(false) call from the SortCommand event.

    Besides, to explicitly force the grid to rebind you should call
    RadGrid1.Rebind();
    instead of DataBind() method.
    For more information on creating grid programmatically you can check the following help article. Also you may find live example here: Grid / On PageLoad

    Let me know how this works for you and if you have any other questions.

    Best wishes,
    Marin
    the Telerik team
    Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ARUN KUMAR
    ARUN KUMAR avatar
    4 posts
    Member since:
    Sep 2010

    Posted 21 Dec 2010 Link to this post

    Hi,

    Even I am facing the same problem. I created dynamc gridboundcolumn but its header text is disappearing after postbacks.

    I used the rebind() method but didnt help.

    Pls provid the solution.

    Regards,

    Arun
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Dec 2010 Link to this post

    Hello Arun,

    Check whether you have added the columns  to the corresponding collection first, before the values for their properties are set.

    C#:
    protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               GridBoundColumn boundColumn;
               boundColumn = new GridBoundColumn();
               RadGrid1.MasterTableView.Columns.Add(boundColumn);
               boundColumn.DataField = "EmployeeID";
               boundColumn.HeaderText = "Emplyee ID";
           }
       }
     
    Thanks,
    Princy.
  6. ARUN KUMAR
    ARUN KUMAR avatar
    4 posts
    Member since:
    Sep 2010

    Posted 21 Dec 2010 Link to this post

    Hello Princy,

    Thanks for the post, the columns are being added but the only thing is that its header text is disappearing after the postback.

    My code snippet is,

    tkgvPerformance.MasterTableView.Columns.Add(

    new GridBoundColumn() { UniqueName = "ColumnName", DataField ="ColumnName", HeaderText = "ColumnName", SortExpression = "ColumnName" });

    Its appearing for the first time its appearing but after the postback it dissappears.

    Thanks and Regards,

    Arun

     

  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Dec 2010 Link to this post

    Hello Arun,

    I have the same issue when I tried your code. Try the following approach to add bound columns to RadGrid.
    C#:
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = "EmployeeID";
    boundColumn.HeaderText = "Emplyee ID";

     I am referring the fololwing documentation. Please give a try with this.
    Programmatic creation

    Thanks,
    Princy.
  8. ARUN KUMAR
    ARUN KUMAR avatar
    4 posts
    Member since:
    Sep 2010

    Posted 22 Dec 2010 Link to this post

    Hi Princy,

    I tried that way too, didnt help.

    Regards,

    Arun
  9. Henrik
    Henrik avatar
    30 posts
    Member since:
    Dec 2010

    Posted 30 Dec 2010 Link to this post

    Bumping this!

    I am having this problem ....
    HeaderText gone as soon as I done a postback, sort etc.

    //h
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Dec 2010 Link to this post

    Hello Henrik,

    If you have the grid declaration is on the aspx page, please make sure that you have dynamically added the column in Page_Load event.When creating the column also you should add immediately the newly created column in the columns collection and then you can set  column properties.

    Thanks,
    Princy.
  11. Henrik
    Henrik avatar
    30 posts
    Member since:
    Dec 2010

    Posted 31 Dec 2010 Link to this post

    Got that working now Princy ...
    Read that in some cases you shall set the props before you dd to the column collection and sometimes after ... But got it right now ...

    //h
  12. Pankaj
    Pankaj avatar
    2 posts
    Member since:
    Oct 2012

    Posted 01 Oct 2012 Link to this post

    Can You send me the Code.. My dynamically created header text is disappearing, when Page is Refreshing.. Could You Please help me in this.. Thanks in Advance..

    Regards,
    Pankaj
  13. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Oct 2012 Link to this post

    Hi,

    Please take a look into the sample code snippet for creating a RadGrid dynamically.
    ASPX:
    <asp:PlaceHolder ID="pl" runat="server"></asp:PlaceHolder>
    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.DataSourceID = "SqlDataSource1";
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
          
        grid.MasterTableView.DataKeyNames = new string[] { "EmployeeID" };
        GridBoundColumn boundColumn1 = new GridBoundColumn();
        boundColumn1.DataField = "EmployeeID";
        boundColumn1.HeaderText = "EmployeeID";
        grid.MasterTableView.Columns.Add(boundColumn1);
     
        boundColumn2 = new GridBoundColumn();
        boundColumn2.DataField = "LastName";
        boundColumn2.HeaderText = "LastName";
        grid.MasterTableView.Columns.Add(boundColumn2);
        this.pl.Controls.Add(grid);
    }

    Thanks,
    Princy.
  14. Pankaj
    Pankaj avatar
    2 posts
    Member since:
    Oct 2012

    Posted 04 Oct 2012 Link to this post

    Hi Princy,
                Thanks, but EnableColumnsViewState="true" if we are keeping in MasterTableView, its working fine..

    Regards,
    Pankaj
  15. Jennifer
    Jennifer avatar
    2 posts
    Member since:
    Jul 2008

    Posted 19 Apr 2013 Link to this post

    It is not the sorting that is causing the problem.  I have narrowed it down and found that if you have the scrolling option on then your header text does not appear.  I turned off the scrolling and it came up fine with my column header text the way it was.
  16. Sagar
    Sagar avatar
    1 posts
    Member since:
    Jun 2016

    Posted 14 Jun Link to this post

    Hi, I have same problem of Header text but with different scenario. I am generating dynamic grid on dropdown's selectedIndexchanged event. But when I am firing itemcommand event Header text are getting invisible. Can anyone help me on this? It's very urgent.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017