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

Accessing specific GridGroupByField Header text programmatically

2 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pmourfield
Top achievements
Rank 1
pmourfield asked on 24 Jul 2012, 12:59 PM
Good morning! I am having trouble attempting to programmatically (via C#) access specific GridGroupByField header texts. I am assigning custom text within the group headers in the following manner in my markup:

<GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="BeginDate" HeaderText="Begin Date" FormatString="{0:d}" />
                            <telerik:GridGroupByField FieldName="EndDate" HeaderText="End Date" FormatString="{0:d}" />
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="BeginDate" SortOrder="Descending" />
                            <telerik:GridGroupByField FieldName="EndDate" SortOrder="Descending" />
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
 </GroupByExpressions>

What I would like to be able to do is access the GridGroupByField HeaderText via C#. Specifically I want to access the EndDate so that I can expand or collapse certain groups based on the end date.

Any help is appreciated! Thank you in advance.


Josh

2 Answers, 1 is accepted

Sort by
0
pmourfield
Top achievements
Rank 1
answered on 25 Jul 2012, 08:11 PM
This is a Solution for the above that I came up with. In my specific case I had to get the group indexes and then grab and parse the text of the second group's header data cell to get a date. Then I had to do a comparison and expand or collapse the group based on the result.

Anyway, one of the reasons why this gave me such a fit in the first place was that I did not know that (and still don't know why) the GridGroupHeaderItems indexes do not increase sequentially (e.g., 1,2,3,4,5,6); rather they increase by twos (e.g., 2,4,6,8). Once I found this out, the rest was cake, so here is my solution in case someone else is attempting to iterate through the GroupHeaderItems and cannot, for the life of them, figure out why that second group isn't expanding or collapsing using an index number of "1".

protected void WeeklyGoalsGrid_PreRender(object sender, EventArgs e)
        {           
            //Iterate through the header items
            foreach (GridGroupHeaderItem item in WeeklyGoalsGrid.MasterTableView.GetItems(GridItemType.GroupHeader))
            {
                //always expand the first group no matter what
                if (item.GroupIndex == "0")
                {
                    item.Expanded = true;                   
                }
                 
                //here I'm parsing out the enddate of the second group's header datacell text then comparing it to see if the date
                //is greater than or between the current date. If not, then expand the second group.
                if (item.GroupIndex == "2")
                {                   
                    string headerText = item.DataCell.Text; //this pulls the entire text of the group header
                    string[] dates = Regex.Split(headerText, "End Date: "); //split on End Date:
                    string endDateStr = dates[1].ToString(); //Grab the End Date
                    DateTime endDate = Convert.ToDateTime(endDateStr); //End Date converted to DateTime datatype
                    DateTime startOfWeek = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
                    DateTime endOfWeek = DateTime.Today.AddDays(+(int)DateTime.Today.DayOfWeek);
                    if (endDate > startOfWeek && endDate < endOfWeek)
                    {
                        item.Expanded = true;
                    }
                }
            }           
        }
0
Martin
Telerik team
answered on 27 Jul 2012, 12:15 PM
Hello Joshua,

Note that the GroupIndex property is not a number actually. It is a string value that represents the grouping hierarchy. For example if you have 2 expressions, the GroupIndex for the nested group items will hold values like "0_1" which would mean "the second child group header item in the first parent group header item.

In addition, you can use another approach to iterate through those items. Here is a sample code:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridGroupHeaderItem)
    {
        GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
        Label1.Text += "Item: " + item.DataCell.Text + " -> Index:" + item.GroupIndex.ToString() + "<br/>";
        if(item.GroupIndex == "0" || item.GroupIndex == "1_0")
        {
            item.Expanded = false;
        }
    }
}

Attached is an image of the output of a sample page with two separate group expressions.

I hope this helps.

Kind regards,
Martin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
pmourfield
Top achievements
Rank 1
Answers by
pmourfield
Top achievements
Rank 1
Martin
Telerik team
Share this question
or