I tried to change in pre render event, data bound etc . No changes affcted. but when post back again the header text changed .. Can you please help about this?
7 Answers, 1 is accepted
/// <summary> |
/// Item databound for the radgrid, set the header text here based on the selected value of the combo |
/// </summary> |
protected void grid_ItemDataBound(object sender, GridItemEventArgs e) { |
if (e.Item is GridHeaderItem) { |
GridHeaderItem item = e.Item as GridHeaderItem; |
item["CustomerID"].Text = combo.SelectedValue; |
} |
} |
/// <summary> |
/// Rebind the grid when the combo changes |
/// </summary> |
/// <param name="o"></param> |
/// <param name="e"></param> |
protected void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) { |
grid.Rebind(); |
} |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title></title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<div> |
<telerik:RadScriptManager ID="scriptMan" runat="server" /> |
<telerik:RadComboBox ID="combo" runat="server" Width="350px" |
AutoPostBack="True" DataSourceID="CustomerDS" DataTextField="CompanyName" |
DataValueField="CustomerID" |
onselectedindexchanged="combo_SelectedIndexChanged"> |
</telerik:RadComboBox> |
<br /><br /><br /><br /><br /> |
<telerik:RadGrid ID="grid" runat="server" DataSourceID="CustomerDS" GridLines="None" |
OnItemDataBound="grid_ItemDataBound"> |
<MasterTableView AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="CustomerDS"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="CustomerID" DefaultInsertValue="" HeaderText="CustomerID" |
ReadOnly="True" SortExpression="CustomerID" UniqueName="CustomerID"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="CompanyName" DefaultInsertValue="" HeaderText="CompanyName" |
SortExpression="CompanyName" UniqueName="CompanyName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="ContactName" DefaultInsertValue="" HeaderText="ContactName" |
SortExpression="ContactName" UniqueName="ContactName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="ContactTitle" DefaultInsertValue="" HeaderText="ContactTitle" |
SortExpression="ContactTitle" UniqueName="ContactTitle"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Address" DefaultInsertValue="" HeaderText="Address" |
SortExpression="Address" UniqueName="Address"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="City" DefaultInsertValue="" HeaderText="City" |
SortExpression="City" UniqueName="City"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Region" DefaultInsertValue="" HeaderText="Region" |
SortExpression="Region" UniqueName="Region"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="PostalCode" DefaultInsertValue="" HeaderText="PostalCode" |
SortExpression="PostalCode" UniqueName="PostalCode"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Country" DefaultInsertValue="" HeaderText="Country" |
SortExpression="Country" UniqueName="Country"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Phone" DefaultInsertValue="" HeaderText="Phone" |
SortExpression="Phone" UniqueName="Phone"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Fax" DefaultInsertValue="" HeaderText="Fax" SortExpression="Fax" |
UniqueName="Fax"> |
</telerik:GridBoundColumn> |
<telerik:GridCheckBoxColumn DataField="Bool" DataType="System.Boolean" DefaultInsertValue="" |
HeaderText="Bool" SortExpression="Bool" UniqueName="Bool"> |
</telerik:GridCheckBoxColumn> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="CustomerDS" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindOAConnectionString %>" |
SelectCommand="SELECT * FROM [Customers]" /> |
</div> |
</form> |
</body> |
</html> |
I have the same issue.
If I change the header text on columns for the radgrid on a postback... eg selecting a drop down list (with autopostback=true), the columns header text does not change until I do an additional postback... so the columns header text is always one step behind.
If I rebind the grid after I have changed the header text, the correct header text is displayed, but I am unable to rebind as users could have entered data into the grid already.
I have tried this on page_load and radgrid_PreRender, where I would expect this to work, but the correct header text is not showing... It seems the header text is simply put into viewstate for next time... very puzzling!
Can anyone help please?
In order to achieve this 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
Changing the header text using the methods in this thread work, BUT it breaks the sorting function for that column. Does anyone know a way around this so I can specify the header text and still be able to sort by the column?
THanks
Charles
You can achieve this requirement using either of these approaches:
1.
protected
void
RadGrid1_DataBinding(
object
sender, EventArgs e)
{
RadGrid1.MasterTableView.GetColumn(
"ShipName"
).HeaderText =
"Success"
;
}
2.
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
TableCell cell = RadGrid1.MasterTableView.GetHeaderCellByColumnUniqueName(
"ShipName"
);
(cell.Controls[0]
as
LinkButton).Text =
"Success"
;
}
I hope this will prove helpful.
Regards,
Eyup
Telerik by Progress
None of the solutions here worked for me but I did end up finding something that worked before the post back and did not break the sorting.
protected void rgMyGrid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
if (e.Column.HeaderText == "HeaderText")
e.Column.HeaderText = "New Header Text";
}