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

Column Headers go blank when sorting

1 Answer 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Iron
Iron
Andrew asked on 18 Jul 2011, 08:02 AM
I have a grid where the columns are created via code, not the markup. When I sort on this grid, the column header text goes blank!
I've isolated this bug into a test project, here's the complete code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadGridDynamicColumns._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title>Sample Rad Grid Example</title>
</head>
<body>
    <form id="form1" runat="server">
<telerik:RadScriptManager runat="server" />
 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="gvwPlayers" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
     
    <telerik:RadGrid
        id="_radGrid"
        runat="server"
        OnNeedDataSource="radGrid_NeedDataSource"
        AllowSorting="true"
        AutoGenerateColumns="false"
        Width="100mm">
    </telerik:RadGrid>
    </form>
</body>
</html>

And the Code-Behind:

using System;
using Telerik.Web.UI;
 
namespace RadGridDynamicColumns
{
    // A test data class
    public class Widget
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
 
 
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._radGrid.Columns.Add(new GridBoundColumn { HeaderText = "Name", DataField = "Name" });
                this._radGrid.Columns.Add(new GridBoundColumn { HeaderText = "Description", DataField = "Description" });
            }
        }
 
        public Widget[] TestData = new[]
                                    {
                                        new Widget { Name = "FooBar", Description = "WidgetFooBar" },
                                        new Widget { Name = "Harold", Description = "Biggles" }
                                    };
        protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            this._radGrid.DataSource = this.TestData;
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Jul 2011, 08:58 AM
Hello Andrew,

When you are adding columns dynamically in PageLoad, grid columns should be added to the Columns collection of the MasterTableView before adding any attributes. Check the following help documentation which explains more about this.
Programmatic Creation.

Thanks,
Princy.
Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Iron
Iron
Answers by
Princy
Top achievements
Rank 2
Share this question
or