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

Dynamically generated columns in radgrid disappear after postback

3 Answers 400 Views
Grid
This is a migrated thread and some comments may be shown as answers.
nader
Top achievements
Rank 1
nader asked on 30 Apr 2015, 12:48 PM
Am using radgrid and creating it in aspx but on certain action i add more GridTemplateColumns to the grid.

    private void CreateDateColumns(List<DateTime> occurenceList)
    {
        if (occurenceList != null && occurenceList.Count > 0)
        {
            int index = 1;
            foreach (DateTime occurence in occurenceList)
            {
                string templateColumnName = occurence.Date.ToShortDateString();
                GridTemplateColumn templateColumn = new GridTemplateColumn();
                templateColumn.ItemTemplate = new MyTemplate(templateColumnName, index);
                grdStudentAttendanceList.MasterTableView.Columns.Add(templateColumn);
                templateColumn.HeaderText = templateColumnName;
                templateColumn.UniqueName = templateColumnName;
                
                index++;
            }
        }
    }

    private class MyTemplate : ITemplate
    {
        protected RadComboBox rcbAttendance;
        private string colname;
        private int _index;
        public MyTemplate(string cName, int index)
        {
            colname = cName;
            _index = index;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            rcbAttendance = new RadComboBox();
            rcbAttendance.Items.Add(new RadComboBoxItem("---Select---", "-1"));
            rcbAttendance.Items.Add(new RadComboBoxItem("Present", "1"));
            rcbAttendance.Items.Add(new RadComboBoxItem("Absent", "2"));
            rcbAttendance.Items.Add(new RadComboBoxItem("Leave", "3"));
            rcbAttendance.ID = "rcbAttendance" + _index;
            container.Controls.Add(rcbAttendance);
        }
    }

All are fine with creation but when i press save button or any combobox make postback the only dynamically generated columns content disappear and the other columns stay.
What i noticed that columns still in place with headertext but only content are disappeared (in my case content are comboboxes)

After enabling viewstate for grid only header text appear.

What should i do to keep columns contents after postback and get their values ?

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 05 May 2015, 08:41 AM
Hi Nader,

If your RadGrid structure will be changed dynamically, you need to follow the exact steps listed in the following help article:
As you will notice, you need to ensure that the columns are added within the Page's Load event handler and that the EnableColumnsViewState property of the MasterTableView is set to false

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
nader
Top achievements
Rank 1
answered on 05 May 2015, 11:40 AM

Hi Konstantin ,

 I solved this issue already with something near what u mentioned, i have to draw the columns every post-back with saving values in each call. Anyway thanks for help

0
Swati
Top achievements
Rank 1
answered on 21 Jul 2017, 01:25 PM

Hi,

Can u please share complete solution or with exact code what you have done.  It will be great help.

 

Thank you in advance.

 

Swati

Tags
Grid
Asked by
nader
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
nader
Top achievements
Rank 1
Swati
Top achievements
Rank 1
Share this question
or