Hi,
I have seen that sets grid properties in PreRender and there is no Rebind but then others (eg. setting header text) that do require ReBind. What are the cases then that will require a call to Rebind for the properties to be set properly?
Why I ask: PreRender is so handy because I can handle all grid localization in one call, for both Programmatic and Declarative grids. Rebinding though causes selected rows to get lost so I cannot use that & the only options other than PreRender are to use a combination of Page_Load, ColumnCreated, ItemCreated, ItemDataBound to properly localize all grid items for Programmatic and Declarative grids.
4 Answers, 1 is accepted
Some grid settings are applied before the the grid is bound. Therefore, setting their corresponding properties in a late event handler like PreRender won't have effect until the next postback.
In order to achieve your original requirement, you will need to change the text of the header cell directly. In order to do that, you can make avail of the GetItems() method:
http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html
In your case it will be something similar to this:
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]
as
GridHeaderItem;
headerItem[
"ShipName"
].Text =
"Success"
;
}
Hope this helps. Please give it a try and let me know if it works for you.
Regards,
Eyup
Telerik
Thanks Eyup - is there any way to access the UniqueName of a GridHeaderItem?
The problem is that I don't always know the names of the columns and I want to localize - how can I iterate through GridHeaderItems and look for a UniqueName?
Hi Eyup,
After some reading I think I have found a solution that will work in PreRender for programmatic and declarative grids, localizing column headers based on iteration and using the UniqueName attribute. I'll post here for anyone that may be interested, please comment if you see any problems with my approach.
GridHeaderItem headerItem = grd.MasterTableView.GetItems(GridItemType.Header)[0]
as
GridHeaderItem;
foreach
(GridColumn column
in
grd.MasterTableView.RenderColumns)
{
if
(column
is
GridBoundColumn)
{
var item = headerItem[column.UniqueName];
if
(item.HasControls() && (item.Controls[0]
is
LinkButton))
{
(item.Controls[0]
as
LinkButton).Text =
"abc"
;
}
else
{
item.Text =
"abc"
;
}
}
}
I'm glad you've managed to find a viable solution for your case.
Using the RenderColumns is the ideal approach. I guess you have Sorting enabled so you need to access the generated header LinkButton. Otherwise, you could simple set the Text property of the cell.
Regards,
Eyup
Telerik