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

Disapearing controls from GridGroupHeaderItem

10 Answers 216 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jan
Top achievements
Rank 1
jan asked on 13 Aug 2008, 11:37 PM
Hello,

I need to add some controls in GridGroupHeaderItems, so I am using following code
    foreach (GridGroupHeaderItem groupHeaderItem in myGrid.MasterTableView.GetItems(GridItemType.GroupHeader)){
        //some code
        groupHeaderItem.DataCell.Controls.Add(myControl);
    }
 in Radgrid DataBound event handler.

 Problem is that after ajax callbacks controls from group header are dismissed with groupHeader.text. Could I somehow prevent this behavior?
 I was trying to place my code to radgrid PreRender event handler, but I am using specific data for constructing controls like this:
    (groupHeaderItem.DataItem as DataRowView)["columnName"].ToString()
 but (groupHeaderItem.DataItem as DataRowView) is a NULL in preRender event handler.
 So - Is there a way how to preserve controls in GridGroupHeaderItems between preRender events?

Thank you
Jan

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Aug 2008, 05:48 AM
Hi Jan,

Try adding the controls to Group Header in the ItemCreated event as shown below.

CS:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridGroupHeaderItem) 
        { 
            GridGroupHeaderItem groupheader = (GridGroupHeaderItem)e.Item; 
            Button btn = new Button(); 
            btn.ID = "Button1"
            btn.Text = "Click"
            groupheader.Controls[0].Controls.Add(btn); 
        } 
    } 


Thanks
Shinu.
0
jan
Top achievements
Rank 1
answered on 14 Aug 2008, 06:46 PM
Thank you,

This was my very first approach. But the problem was that I nead to access data in groupheader.getchilditems and  (groupHeaderItem.DataItem as DataRowView), so this was not realizable in ItemCreated, but not even in grid PreRender event handlers.
 My contemporary solution is to make add controls in grid's preRender event handler - so that controls are added each time the grid is rendered (and so they can not "vanish"), here I can also access groupheader.getchilditems array, and data from group header ((groupHeaderItem.DataItem as DataRowView) - which could be null in preRender), I get by parsing the groupHeaderItem.DataCell.Text string.
0
Dany Thielen
Top achievements
Rank 1
answered on 18 Aug 2008, 08:33 AM
Hello,

I think I have more or less the same problem:
In the group header I want to add a linkbutton which needs as text property a value which I can get from
(groupHeaderItem.DataItem as DataRowView)["columnName"]
. I used the example given by Shinu for my implementation, however, since I need to access the DataItem of the GridGroupHeaderItem, I used OnItemDataBound event instead of OnItemCreated event. This works fine when the RadGrid is first bound, but after expanding or collapsing a group, the button disappears and GroupHeader shows the default names of the groupbyexpressions.

Jan's solution sounds interesting, however it would be very nice having a more direct solution.

Thank you

Dany

0
Yavor
Telerik team
answered on 19 Aug 2008, 05:26 AM
Hi Dany,

You can use the PreRender event handler, to access the group header items collection. All the data for the items will be available, and you will also be able to add the new items to the group header.

All the best,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Dany Thielen
Top achievements
Rank 1
answered on 19 Aug 2008, 07:48 AM
Hi Yavor

Thank you for your prompt reply :)
I tried your solution, but however there still is a small problem: I want to attach a command to the link button, that I am adding to the GridGroupHeader. If I add the link button in the PreRender method, I cannot trigger the command by clicking on the link button :(
However, if I add the link button in the Load method it works and I also have access to all the data through parsing the item.DataCell.Text property.  But I am not sure if this is the way it is supposed to work? I am wondering if it will still be a valid solution if Rad Controls are updated to a new version? Is there no solution which will be more "resistent", in terms of using one of the Rad-defined events for RadGrid and accessing information  in a more object-oriented like way than doing a parsing of a string?

Thanks again for your answer,
Best greetings,

Dany
0
bern
Top achievements
Rank 1
answered on 28 Jul 2009, 06:04 PM

I'm having the same problem!  During a postback (triggered by another button in the page) the DataRowView is NULL in the ItemCreated event.  The value NULL prevents me from determining which controls to add to this group header.  Like in the sample provided by

Shinu Im calling my own CreateHeaderControls(e) in two places ItemCreated and ItemDataBound.  Telerik team member Yavor suggested using the PreRender event but did not provide sample code so I still don't have a solution.

//Note how my function relies on the DataRowView.

 

private

 

void CreateHeaderControls(GridItemEventArgs e)

 

{

 

if (e.Item is GridGroupHeaderItem)

 

{

 

GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;

 

 

DataRowView groupDataRow = (DataRowView)e.Item.DataItem;

 

 

if (String.IsNullOrEmpty(groupDataRow[1].ToString()))

 

item.Display =

false;

 

 

else

 

{

 

Label lblGroupHeaderName = new Label();

 

lblGroupHeaderName.Font.Bold =

true;

 

lblGroupHeaderName.Text = groupDataRow[1].ToString();

item.DataCell.Controls.Add(lblGroupHeaderName);

 

if (groupDataRow[1].ToString() == "Regular Time Worked")

 

{

 

Button btnAddRegularShift = new Button();

 

btnAddRegularShift.OnClientClick =

"AddRegular2Shift();return false;";

 

btnAddRegularShift.Text =

"Add Regular Shift";

 

item.DataCell.Controls.Add(btnAddRegularShift);

}

 

else if (groupDataRow[1].ToString() == "OnCall Time Worked")

 

{

 

Button btnAddOnCallShift = new Button();

 

btnAddOnCallShift.OnClientClick =

"AddOnCallShift();return false;";

 

btnAddOnCallShift.Text =

"Add OnCall";

 

item.DataCell.Controls.Add(btnAddOnCallShift);

}

}

}


Anyone having the sample problem??>>>>Please help!

Bernard

 

 

 

0
Yavor
Telerik team
answered on 31 Jul 2009, 02:01 PM
Hi bern,

The PreRender event handler can also handle the task at hand, as Jan suggested in the third post on the matter. To get all the group items you can use code like:

foreach (GridGroupHeaderItem groupHeaderItem in myGrid.MasterTableView.GetItems(GridItemType.GroupHeader))

Adding the additional elements in the PreRender event handler will enure that they are persisted as expected.

Sincerely yours,
Yavor
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
Alexis
Top achievements
Rank 1
answered on 31 Jul 2009, 03:08 PM
Hello,
I have the exact same problem. I new to add a "Add new" button in each groupheader of a grid.

Can't use the ItemCreated event because I don't have acces to the data.. (Obviously). That mean i can't put the right commandName for the button.

Can't use the ItemDataBound event because I have this disepearing control effect when grid is recreated from viewstate.
Can't use the PreRender event because hooking an event handler or a CommandName in it doesn't seems to work.

Anyone have a clear solution for this?

Thank you!
0
bern
Top achievements
Rank 1
answered on 31 Jul 2009, 05:11 PM

PreRender doesnt solve the problem.  Again like the ItemCreated on a postback the DataItem is null. 
DataRowView groupDataRow = (DataRowView)groupHeaderItem.DataItem

 

0
Yavor
Telerik team
answered on 04 Aug 2009, 02:13 PM
Hi,

Indeed, adding the button in the PreRender event handler will not allow the command/server side event handler for the click to be raised. Another option in this case would be to add a client side onclick event handler. There, from the client, you can make an AjaxRequest to the server, and handle the relevant logic there.
I hope this suggestion helps.

Regards,
Yavor
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.
Tags
Grid
Asked by
jan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
jan
Top achievements
Rank 1
Dany Thielen
Top achievements
Rank 1
Yavor
Telerik team
bern
Top achievements
Rank 1
Alexis
Top achievements
Rank 1
Share this question
or