Change column text like XAML converters

2 posts, 0 answers
  1. danparker276
    danparker276 avatar
    389 posts
    Member since:
    Aug 2010

    Posted 25 Mar 2013 Link to this post

    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.
  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 28 Mar 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top