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

Traversing GroupHeaderItems in PreRender

10 Answers 337 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 17 Mar 2009, 09:16 PM
Can anyone please help me with how to traverse and change the GroupHeaderItems in the PreRender event.  I know how to do this in the ItemDataBount event with the below code, but this won't work because everytime a user closes a grouping the changes I make to the groupheaderitem go away.

if (e.Item is GridGroupHeaderItem)  
        {  
            GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;  
            string grpIndx = item.GroupIndex.ToString();  
 
            if (grpIndx.Length >= 5)  
            {  
                if (grpIndx.Substring(grpIndx.Length - 2) == "_0")  
                {  
                    DataRowView groupDataRow = (DataRowView)e.Item.DataItem;  
 
                    // Make sure a project exists by checking the Project Id group value  
                    if (string.IsNullOrEmpty(groupDataRow.Row["ProjectId"].ToString()))  
                    {  
                        // Hide the Project grouping  
                        item.Visible = false;  
                    }  
                    else  
                    {  
                        if (groupDataRow.Row["ReleasedByProject"].ToString().ToUpper() != "TRUE") // (!ois.ReleasedByProject)  
                        {  
                            HyperLink lnk = new HyperLink();  
                            lnk.ID = "btnProjRelease";  
                            lnk.Text = "Click To Release";  
                            lnk.ToolTip = "Project Release";  
                            lnk.NavigateUrl = "#";  
                            lnk.Attributes["onclick"] = string.Format("return RunProjectRelease('{0}');", groupDataRow.Row["ProductId"].ToString());  
                            lnk.Enabled = _isReleasedByAccounting && _canCompleteSteps;  
 
                            Label lbl = new Label();  
                            lbl.Text = string.Format("Project: {0}:  ", groupDataRow.Row["ProjectName"].ToString());  
 
                            item.DataCell.Controls.Add(lbl);  
                            item.DataCell.Controls.Add(lnk);  
                        }  
                    }  
                }  
            }  
        } 

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Mar 2009, 03:45 AM
Hi Craig,

Try the following approach to loop through the Group header in the PreRender event.

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridGroupHeaderItem groupHeader in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)) 
        { 
            string grpIndx = groupHeader.GroupIndex.ToString();   
            //... 
        } 
        
    } 


Thanks
Shinu
0
Princy
Top achievements
Rank 2
answered on 18 Mar 2009, 03:49 AM

0
Craig
Top achievements
Rank 1
answered on 18 Mar 2009, 04:05 PM
Thanks for your reply Shinu.  Your suggestion worked great during the initial loading of the screen.  However, whenever an AJAX request is sent back and the pre-render runs again the DataItem of the groupHeader is NULL.  Is there a way around this?  I need to access the data of the groupheader so I can determine what to put in the groupheader.

Thanks
0
Iana Tsolova
Telerik team
answered on 20 Mar 2009, 08:30 AM
Hello Craig,

Indeed, the DataItem property is available only during data binding. You can access/use it in the ItemDataBound event if it works for you. On RadGrid PreRender, you can get only the text of the GridGroupHeader.

Regards,
Iana
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Craig
Top achievements
Rank 1
answered on 20 Mar 2009, 08:14 PM
Iana -

Thanks for the response.  Since the dataitem is not accessible during the prerender event of rad grid how would you suggest I customize the group header item?  I am trying to create a custom link in the group header item that is dependant on the data.  I can put it in the itemdatabound event and it works great, except whenever an ajax even fires then it goes away.  So I read on other forums to put it in the prerender event, except I can't get access to the dataitem.  Any suggestions?

Is there a way to not allow the expanding / collapsing of group sections?

Thanks
0
Raj
Top achievements
Rank 1
answered on 20 Mar 2009, 08:53 PM

Hi Craig,

 

I have done a similar app and able to access the DataItem in pre-render :

Here's the code extract:

 

 

 

0
Craig
Top achievements
Rank 1
answered on 20 Mar 2009, 08:58 PM
Raj -

I am very interested in your code but can't read it.  Can you please re-post it using the format code block thing so it is copied cleanly?

Thanks
0
Accepted
Raj
Top achievements
Rank 1
answered on 20 Mar 2009, 08:58 PM
sorry, messed up with Format Code Block ,
pasting it again:

 

protected void rgrdBudgetData_PreRender(object sender, EventArgs e)

 

{

 

    foreach (GridGroupHeaderItem grpHdrItem in rgrdBudgetData.MasterTableView.GetItems(GridItemType.GroupHeader))

    {

 

         Double grpProjCost = 0.0; 
 

 

        TraverseGridGroup(grpHdrItem,

out grpProjCost, out grpAdjAmt, out grpProjValue, out grpBudgetVar);

 

 

        TableCell cell = new TableCell();

 

         cell.ColumnSpan = 1;

        cell.Text = grpProjCost.ToString(

"C");

 

         grpHdrItem.Cells.Add(cell);

    }

}

 

 

void TraverseGridGroup(GridGroupHeaderItem grpHdrItem,out Double grpProjCost,out Double grpAdjAmt,out Double grpProjValue,out Double grpBudgetVar)

{

    grpProjCost = 0.0;

 

 

    GridItem[] children = grpHdrItem.GetChildItems();

     

foreach (GridItem item in children)

 

     {

 

        if (item is GridGroupHeaderItem)

 

        {

             

Double subgrpProjCost = 0.0;

 

             

GridGroupHeaderItem subgrpHdrItem = item as GridGroupHeaderItem;

 

             TraverseGridGroup(subgrpHdrItem,

out subgrpProjCost, out subgrpAdjAmt, out subgrpProjValue, out subgrpBudgetVar);

 

             grpProjCost += subgrpProjCost;

        }

         

else if (item is GridDataItem)

 

         {

             

GridDataItem dataItem = item as GridDataItem;

 

             

DataRowView dRow = dataItem.DataItem as DataRowView;

 

             

Double revBudget = ConvertDB.ToDouble(dRow["REVISEDBUDGET"]);

 

             revBudget = revBudget ==

Double.MinValue ? 0 : revBudget;

 

             

Double ytdExpense = ConvertDB.ToDouble(dRow["YTDEXPENSE"]);

 

             ytdExpense = ytdExpense ==

Double.MinValue ? 0 : ytdExpense;

 

             

Double encumbrance = ConvertDB.ToDouble(dRow["Z_NET_ENC_BAL"]);

 

             encumbrance = encumbrance ==

Double.MinValue ? 0 : encumbrance;

 

             

Double totalExpense = ytdExpense + encumbrance;

 

             

Double projValue = 0.0;

 

             

if(!ConvertDB.ToBoolean(dRow["NoProjection"]))

 

             {

                 

if (ConvertDB.ToBoolean(dRow["ProjectByMonth"]))

 

                 {

                    projValue = totalExpense / 12 * ProjectionFactor;

                }

                 

else

 

                 

{

 

                    projValue = (totalExpense / PayPeriodsYTD) * (TotalPeriodsInYear - PayPeriodsYTD);

                }

            }

             

Double adjProjAmount = ConvertDB.ToDouble(dRow["AdjAmount"]);

 

            adjProjAmount = adjProjAmount ==

Double.MinValue ? 0 : adjProjAmount;

 

             

Double totalProjValue = 0.0;

 

             totalProjValue = totalExpense + projValue + adjProjAmount;

             

grpProjValue += totalProjValue;

 

        }

    }

}

 



0
Craig
Top achievements
Rank 1
answered on 08 Apr 2009, 06:02 PM
I have figured out the best way to do this.  Attached is the code for both the Pre-Render and the traverse group header function.  This works for initial page load and all ajax call backs.

protected void grdInitialInput_PreRender(object sender, EventArgs e)  
    {  
        foreach (GridGroupHeaderItem item in grdInitialInput.MasterTableView.GetItems(GridItemType.GroupHeader))  
        {  
            string grpIndx = item.GroupIndex.ToString();  
            if (grpIndx.Length >= 5)  
            {  
                if (grpIndx.Substring(grpIndx.Length - 2) == "_0")  
                {  
                    GridDataItem dtaItem = TraverseGridGroupForDataItem(item);  
 
                    if (dtaItem != null)  
                    {  
                        HyperLink lnk = new HyperLink();  
                        lnk.ID = "lnkAccountCreditScreen";  
                        lnk.Text = "Change Accounting Limits";  
                        lnk.NavigateUrl = "#";  
                        lnk.Attributes["onclick"] = string.Format("return ShowAccountCreditScreen('{0}', '{1}');", dtaItem["AccountId"].Text, lnk.ClientID);  
 
                        item.DataCell.Controls.Add(lnk);  
                    }  
                }  
            }  
        }  
    }  
 
    private GridDataItem TraverseGridGroupForDataItem(GridGroupHeaderItem grpHdrItem)  
    {  
        GridDataItem dataItem = null;  
 
        foreach (GridItem item in grpHdrItem.GetChildItems())  
        {  
            if (item is GridGroupHeaderItem)  
            {  
                GridGroupHeaderItem subgrpHdrItem = item as GridGroupHeaderItem;  
 
                TraverseGridGroupForDataItem(subgrpHdrItem);  
            }  
            else if (item is GridDataItem)  
            {  
                dataItem = item as GridDataItem;  
            }  
        }  
 
        return dataItem;  
    } 
0
Jason
Top achievements
Rank 1
answered on 08 Jul 2009, 01:34 AM
protected void rgrdBudgetData_PreRender(object sender, EventArgs e) 
    foreach (GridGroupHeaderItem grpHdrItem in rgrdBudgetData.MasterTableView.GetItems(GridItemType.GroupHeader)) 
    { 
        Double grpProjCost = 0.0;  
 
        TraverseGridGroup(grpHdrItem, out grpProjCost, out grpAdjAmt, out grpProjValue, out grpBudgetVar); 
 
        TableCell cell = new TableCell(); 
        cell.ColumnSpan = 1; 
        cell.Text = grpProjCost.ToString("C"); 
        grpHdrItem.Cells.Add(cell); 
    } 
 
void TraverseGridGroup(GridGroupHeaderItem grpHdrItem,out Double grpProjCost,out double grpAdjAmt,out Double grpProjValue,out Double grpBudgetVar) 
    grpProjCost = 0.0; 
 
    GridItem[] children = grpHdrItem.GetChildItems(); 
 
    foreach (GridItem item in children) 
    { 
        if (item is GridGroupHeaderItem) 
        { 
            Double subgrpProjCost = 0.0; 
            GridGroupHeaderItem subgrpHdrItem = item as GridGroupHeaderItem; 
            TraverseGridGroup(subgrpHdrItem, out subgrpProjCost, out subgrpAdjAmt, out subgrpProjValue, out subgrpBudgetVar); 
            grpProjCost += subgrpProjCost; 
        } 
        else if (item is GridDataItem) 
        { 
            GridDataItem dataItem = item as GridDataItem; 
            DataRowView dRow = dataItem.DataItem as DataRowView; 
 
            Double revBudget = ConvertDB.ToDouble(dRow["REVISEDBUDGET"]); 
            revBudget = revBudget == Double.MinValue ? 0 : revBudget; 
            Double ytdExpense = ConvertDB.ToDouble(dRow["YTDEXPENSE"]); 
            ytdExpense = ytdExpense == Double.MinValue ? 0 : ytdExpense; 
            Double encumbrance = ConvertDB.ToDouble(dRow["Z_NET_ENC_BAL"]); 
            encumbrance = encumbrance == Double.MinValue ? 0 : encumbrance; 
            Double totalExpense = ytdExpense + encumbrance; 
            Double projValue = 0.0; 
 
            if(!ConvertDB.ToBoolean(dRow["NoProjection"])) 
            { 
                if (ConvertDB.ToBoolean(dRow["ProjectByMonth"])) 
                { 
                     projValue = totalExpense / 12 * ProjectionFactor; 
                } 
                else 
                { 
                     projValue = (totalExpense / PayPeriodsYTD) * (TotalPeriodsInYear - PayPeriodsYTD); 
                } 
            } 
 
            Double adjProjAmount = ConvertDB.ToDouble(dRow["AdjAmount"]); 
            adjProjAmount = adjProjAmount == Double.MinValue ? 0 : adjProjAmount; 
 
            Double totalProjValue = 0.0; 
            totalProjValue = totalExpense + projValue + adjProjAmount; 
            grpProjValue += totalProjValue; 
        } 
    } 
 

Thought I'd try to make this easier to read :)
Tags
Grid
Asked by
Craig
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Craig
Top achievements
Rank 1
Iana Tsolova
Telerik team
Raj
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Share this question
or