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

Losing Column States

2 Answers 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin White
Top achievements
Rank 2
Kevin White asked on 07 Jul 2008, 06:32 PM

I have a simple page where I am dynamically generating columns on the page load event and seting datasource during the need data source event.  The initial load works correctly, but after any postback the columns still exist but the header text and datafield properties are empty.

Below is the simpliest example I can make. Any thoughts on why the columns are losing their information?

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    public class DataClass
    {
        private string _a = "";
        private string _b = "";
        public string A { get { return this._a; } }
        public string B { get { return this._b; } }
    }  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            System.Reflection.PropertyInfo[] fields = typeof(DataClass).GetProperties();
            foreach (System.Reflection.PropertyInfo field in fields)
            {
                GridBoundColumn c = new GridBoundColumn();
                c.DataField = field.Name;
                c.HeaderText = field.Name;
                this.RadGrid1.MasterTableView.Columns.Add(c);
            }
        }
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        System.Collections.Generic.List<DataClass> results = new System.Collections.Generic.List<DataClass>();
        this.RadGrid1.DataSource = results;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Column exist but header text and data field properties are ""
    }   
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Force Postback" onclick="Button1_Click" />       
        <telerik:RadGrid ID="RadGrid1" runat="server" onneeddatasource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" />
    </form>
</body>
</html>

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Jul 2008, 04:41 AM
Hi Kevin,

You need to add column to the columns collection immediately after the column creation:

GridBoundColumn c = new GridBoundColumn();
this.RadGrid1.MasterTableView.Columns.Add(c);

c.DataField = field.Name;
c.HeaderText = field.Name;
You can check this example for more info:
http://www.telerik.com/DEMOS/ASPNET/Prometheus/Grid/Examples/Programming/GroupBy/DefaultCS.aspx

All the best,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Shinu
Top achievements
Rank 2
answered on 08 Jul 2008, 05:13 AM
Hi Kevin,

You can also have a look at the following help article.
Dynamically defining the structure of a statically-declared grid

Thanks
Shinu.


Tags
Grid
Asked by
Kevin White
Top achievements
Rank 2
Answers by
Vlad
Telerik team
Shinu
Top achievements
Rank 2
Share this question
or