RadGrid / C# / ASP.NET - Changing column text on DataBound

2 posts, 0 answers
  1. Chema
    Chema avatar
    4 posts
    Member since:
    Feb 2012

    Posted 09 Apr 2012 Link to this post

    I want to change the text displayed on a rad grid based on the current text it had
    here is the code-behind in C#
    EXAMPLE A
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)e.Item;
     
                    //Edits the text value of the column "Flow"
                    if (dataItem["Flow"].Text == "1")
                    {
                        dataItem["Flow"].Text = "Outcome";
                    }
                    if (dataItem["Flow"].Text == "2")
                    {
                        dataItem["Flow"].Text = "Income";
                    }
                }
            }

    An awkward workaround I found is to use a hidden column replica like this
    EXAMPLE B
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)e.Item;
     
                    //Edits the text value of the column "Flow"
                    if (dataItem["FlowReplica"].Text == "1")
                    {
                        dataItem["Flow"].Text = "Outcome";
                    }
                    if (dataItem["FlowReplica"].Text == "2")
                    {
                        dataItem["Flow"].Text = "Income";
                    }
                }
            }

    This works nice, but not so nice when you are working with over 10 columns
    that would make 10 more hidden replicas
    Is there another way to make it work as I desired in the EXAMPLE A
    thanks in advance
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Apr 2012 Link to this post

    Hello Chema,

    I cannot find anything wrong with your first approach(Example A). There is no need to access the columns using a hidden column.
    Since ItemDataBound event will fire each time, you can check for the condition and change the column text dynamically.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
     if (e.Item is GridDataItem)
     {
      GridDataItem item = (GridDataItem)e.Item;
      if (item["EmployeeID"].Text == "1")//where dataItem is object of type GridDataItem
      {
        item["EmployeeID"].Text = "Id";
      }
     }
    }

    Thanks,
    Princy.
Back to Top