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

[Solved] AutoGenerateColumns with server-side custom column groupings

3 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 23 Jun 2014, 10:46 PM
I can do dynamic server-side column groupings ONCE, but then can't get the grid to 're-set' for different groupings unless I change the AutoGenerateColumns to FALSE & generate the columns on the server-side.

The SECOND time (when a new Start Date is selected), I get the error: "No column group with the specified group name ( Mon_06-23 ) exists!"
(where Mon_06-23 is an 'old' grouping from the previous viewing)

Apparently I am not doing enough to "clear out the grid" before re-generating it with new Column Groupings.

My grid HTML is:

<telerik:RadGrid ID="RadGridWkSched" runat="server" CellSpacing="0" GridLines="Vertical" AutoGenerateColumns="True"
    OnItemDataBound="RadGridWkSched_ItemDataBound" OnColumnCreated="RadGridWkSched_ColumnCreated">
</telerik:RadGrid>

My Server-side code is:

        private void FillScheduleWeekGrid()
        {
            // get the Start date from the input control
            DateTime dtstart = DateTime.Parse(RadDatePickerWeek.SelectedDate.ToString());

            // clear out the grid before re-populating it
            currentSection = "";
            RadGridWkSched.Columns.Clear();
            RadGridWkSched.MasterTableView.ColumnGroups.Clear();

            for (int i = 0; i < 7; i++)
            {
                DateTime thisdt = dtstart.AddDays((double)i);
                string weekDate = thisdt.ToString(ScheduleController.DateFormat);

                // declare a column group for this date
                GridColumnGroup colGroupDate = new GridColumnGroup();
                RadGridWkSched.MasterTableView.ColumnGroups.Add(colGroupDate);   // need to add it before setting the values
                colGroupDate.Name = colGroupDate.HeaderText = weekDate;
                colGroupDate.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            }
            RadGridWkSched.DataSource = scheduleController.FillScheduleWeekDT(dtstart, serviceLevelID, Session["version"].ToString());
            RadGridWkSched.DataBind();
            }
        }






3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 26 Jun 2014, 01:56 PM
Hi Scott,

When the AutoGenerateColumns property is set to true you could use the AutoGeneratedColumns property to get an array of the columns in RadGrid. You could find more information on the property here.

Also I inspected the provided code and noticed that DataBind() method is used to bind the grid. Please note that this method is used with simple data binding. As the name suggests, this data binding is intended to be used in simple scenarios where more advanced functionalities like grouping, sorting, filtering, etc. are not needed for RadGrid.

It is recommended to implement advanced data binding by using either declarative data source or handling the NeedDataSource event. If you would like more information on the two approaches you would find the following articles interesting:

Declarative DataSource
Advanced Data-binding (Using NeedDataSource Event)

This being said, would you elaborate more on when are you calling the
FillScheduleWeekGrid() method? Also, would you describe in more detail the functionality that you would like to achieve? What is your requirement and what should be the expected result?

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott
Top achievements
Rank 1
answered on 26 Jun 2014, 05:40 PM
Viktor, I will look at the links you sent, but I would like to know why I can't get the old groupings to 'clear out' correctly, since that is the only thing that fails.  To answer your request for more detail, that screen has a calendar control, and when it is used to set the start date I want to re-load the grid with new column groupings for the 7 days after.  Here's the calendar control and the code for calling FillScheduleWeekGrid:

<telerik:RadDatePicker ID="RadDatePickerWeek" runat="server" Culture="en-US" AutoPostBack="True"
     DateInput-Label="StartDate:" OnSelectedDateChanged="SelectedCalendarChanged" Skin="Web20">
    <Calendar ID="Calendar1" UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x"
        Skin="Web20" runat="server">
    </Calendar>
    <DateInput ID="DateInput1" DisplayDateFormat="M/d/yyyy" DateFormat="M/d/yyyy" AutoPostBack="True"
        runat="server">
    </DateInput>
    <DatePopupButton></DatePopupButton>
</telerik:RadDatePicker>

        protected void Page_Init(object sender, EventArgs e)
        {
            // empty the grid
            RadGridWkSched.DataSource = null;
            RadGridWkSched.Rebind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillParamControls();
                // fill & bind the data grid
                FillScheduleWeekGrid();
            }
        }
        protected void SelectedCalendarChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            FillScheduleWeekGrid();
        }



0
Viktor Tachev
Telerik team
answered on 01 Jul 2014, 10:21 AM
Hello Scott,

In the provided code I noticed that the Rebind() method is called for the RadGrid. In addition DataBind() is used in the FillScheduleWeekGrid() method. Please note that such scenario is not supported. The Rebind() method triggers NeedDataSource for the RadGrid and DataBind() is used for simple data binding.

Try removing all calls to DataBind() and use only advanced data binding for the grid and you should be able to achieve the functionality you are looking for.

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Scott
Top achievements
Rank 1
Share this question
or