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

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

1 Answer 578 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chema
Top achievements
Rank 1
Chema asked on 10 Apr 2012, 12:30 AM
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

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Apr 2012, 06:48 AM
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.
Tags
General Discussions
Asked by
Chema
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or