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

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

14 Answers 704 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 19 Nov 2010, 12:20 AM
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

14 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 24 Nov 2010, 10:14 AM
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.
0
ARUN KUMAR
Top achievements
Rank 1
answered on 21 Dec 2010, 07:17 AM
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
0
Princy
Top achievements
Rank 2
answered on 21 Dec 2010, 08:53 AM
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.
0
ARUN KUMAR
Top achievements
Rank 1
answered on 21 Dec 2010, 10:20 AM
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

 

0
Princy
Top achievements
Rank 2
answered on 22 Dec 2010, 09:54 AM
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.
0
ARUN KUMAR
Top achievements
Rank 1
answered on 22 Dec 2010, 10:23 AM
Hi Princy,

I tried that way too, didnt help.

Regards,

Arun
0
Henrik
Top achievements
Rank 1
answered on 30 Dec 2010, 05:41 PM
Bumping this!

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

//h
0
Princy
Top achievements
Rank 2
answered on 31 Dec 2010, 01:22 PM
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.
0
Henrik
Top achievements
Rank 1
answered on 31 Dec 2010, 04:27 PM
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
0
Pankaj
Top achievements
Rank 1
answered on 01 Oct 2012, 12:26 PM
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
0
Princy
Top achievements
Rank 2
answered on 03 Oct 2012, 12:09 PM
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.
0
Pankaj
Top achievements
Rank 1
answered on 04 Oct 2012, 08:34 AM
Hi Princy,
            Thanks, but EnableColumnsViewState="true" if we are keeping in MasterTableView, its working fine..

Regards,
Pankaj
0
Jennifer
Top achievements
Rank 1
answered on 19 Apr 2013, 07:18 PM
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.
0
Sagar
Top achievements
Rank 1
answered on 14 Jun 2016, 05:43 AM
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.
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Marin
Telerik team
ARUN KUMAR
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Henrik
Top achievements
Rank 1
Pankaj
Top achievements
Rank 1
Jennifer
Top achievements
Rank 1
Sagar
Top achievements
Rank 1
Share this question
or