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

Change column text like XAML converters

1 Answer 36 Views
Grid
This is a migrated thread and some comments may be shown as answers.
danparker276
Top achievements
Rank 2
danparker276 asked on 25 Mar 2013, 07:46 PM
I want to change the column's text like an enum from an int to a value.  For example I get a data field "CompanyId" for 1 I want to display "ACME Inc" for 2 I want to display "Bike Shop"
Now I could change it on the object I've sent over, but I've been living in the XAML world for a while and I was wondering if there was an easy way to convert it.  I guess almost like a calculated column but that looks like it's only for numbers.

1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 28 Mar 2013, 12:27 PM
Hi Dan,

If you want to change the HeaderText of each column you can do that on ColumnCreating or ColumnCreated events depending on that it columns are auto generated or not. For example:
void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        GridBoundColumn column = e.Column as GridBoundColumn;
        if (column != null)
        {
            column.HeaderText = columnNames[column.HeaderText];
        }
    }
 
    void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("1");
        columnNames.Add("1", "ID");
        table.Columns.Add("2");
        columnNames.Add("2", "Item");
        table.Columns.Add("3");
        columnNames.Add("3", "Date");
        for (int i = 1; i < 10; i++)
        {
            table.Rows.Add(i, "Item" + i.ToString(), DateTime.Now.AddDays(i));
        }
 
        RadGrid1.DataSource = table;
    }

Where columnNames is a dictionary which contains all number/text pairs:
public Dictionary<string, string> columnNames = new Dictionary<string, string>();
If you want to change the exact values in each data cell the right place is RadGrid.ItemDataBound event, where you can get an object to which the row is bound via
(e.Item as GridDataItem).DataItem property. Then you can convert the values into this object properties and assign them back to the (e.Item as GridDataItem)[“ColumnUniqueName”].Text property.

Please give it try and let me know if you experience any problems.

Looking forward for your reply.


All the best,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
danparker276
Top achievements
Rank 2
Answers by
Radoslav
Telerik team
Share this question
or