Hi,
I am building my columns as below, but the caption remains as "MYCOL" as opposed to "My Column", this seems incorrect?
I am building my columns as below, but the caption remains as "MYCOL" as opposed to "My Column", this seems incorrect?
DataColumn dc = new DataColumn("MYCOL", System.String);
dc.Caption = "My Column";
dt.Columns.Add(dc);
3 Answers, 1 is accepted
0
Hi Al,
With auto-generated columns, RadGrid will use the column's name for the UniqueName and the HeaderText of the generated columns. If you need to use the caption of the columns instead you can handle the server-side OnColumnCreated event, get reference to the DataTable set as a DataSource of the grid, traverse through the columns collection and manually set their caption as a HeaderText of the grid's columns:
And the code-behind:
Hope this helps.
Regards,
Konstantin Dikov
Telerik
With auto-generated columns, RadGrid will use the column's name for the UniqueName and the HeaderText of the generated columns. If you need to use the caption of the columns instead you can handle the server-side OnColumnCreated event, get reference to the DataTable set as a DataSource of the grid, traverse through the columns collection and manually set their caption as a HeaderText of the grid's columns:
<
telerik:RadGrid
runat
=
"server"
ID
=
"RadGrid1"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnColumnCreated
=
"RadGrid1_ColumnCreated"
>
</
telerik:RadGrid
>
And the code-behind:
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
DataTable table =
new
DataTable();
DataColumn dc =
new
DataColumn(
"ID"
,
typeof
(String));
dc.Caption =
"test caption"
;
table.Columns.Add(dc);
for
(
int
i = 0; i < 5; i++)
{
table.Rows.Add(i);
}
(sender
as
RadGrid).DataSource = table;
}
protected
void
RadGrid1_ColumnCreated(
object
sender, GridColumnCreatedEventArgs e)
{
if
(e.Column
is
IGridDataColumn)
{
IGridDataColumn column = e.Column
as
IGridDataColumn;
string
columnName = column.GetActiveDataField();
RadGrid grid = sender
as
RadGrid;
if
(grid.DataSourceObject !=
null
)
{
DataTable table = grid.DataSource
as
DataTable;
foreach
(DataColumn dColumn
in
table.Columns)
{
if
(dColumn.ColumnName == columnName)
{
e.Column.HeaderText = dColumn.Caption;
}
}
}
}
}
Hope this helps.
Regards,
Konstantin Dikov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 11 Dec 2014, 06:35 AM
Thanks Konstantin, that worked perfectly
0
Julian
Top achievements
Rank 1
answered on 05 May 2015, 05:31 PM
OMG I've been looking for this for a week. This works great thanks!!