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

Column caption not being set

3 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Iron
Iron
Iron
Al asked on 08 Dec 2014, 12:22 PM
Hi,
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

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Dec 2014, 01:19 PM
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:
<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!!
Tags
Grid
Asked by
Al
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Al
Top achievements
Rank 1
Iron
Iron
Iron
Julian
Top achievements
Rank 1
Share this question
or