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

Change grid values on itemdatabound

5 Answers 1987 Views
Grid
This is a migrated thread and some comments may be shown as answers.
newbie
Top achievements
Rank 1
newbie asked on 08 Jul 2010, 10:44 PM
I have a grid bound to a datasource.
However, I have a settings page where the user can specify the number of characters to be displayed for any field in the grid.

I want to trim the column value based on that setting before the grid is displayed.
I was trying to do that in the ItemDataBound event.

Any suggestions how I can do that.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jul 2010, 07:39 AM
Hello,

One approach is by using Substring() method , you can display a specified number of characters in a cell value.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       int count=3;    //count is the no of characters that should be displayed
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           string CategoryName = item["ColumnUniqueName"].Text;
           item["ColumnUniqueName"].Text = CategoryName.Substring(0, count);
      }
  }

Thanks,
Princy.

0
newbie
Top achievements
Rank 1
answered on 09 Jul 2010, 06:02 PM
Hi Princy.

The issue i am having here is in my ItemDataBound event item["ColumnUniqueName"].Text returns empty string.
But I know there is a value because when I do  the below I can see the values.

DataRowView groupDataRow = (DataRowView)e.Item.DataItem;     
string text = groupDataRow.Row.ItemArray[i].ToString();

 I am using a template column as below. Is that the reason why item["ColumnUniqueName"] is returned blank?
<telerik:GridTemplateColumn UniqueName="Status" HeaderText="Status" Visible="false"
                                    DataField="Status" AllowFiltering="true">
                                    <ItemStyle BorderStyle="None"></ItemStyle>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <div>
                                            <%# Eval("Status")%>
                                        </div>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
0
Princy
Top achievements
Rank 2
answered on 12 Jul 2010, 08:00 AM
Hello,

Since the TemplateColumn is rendered after ItemDataBound event, you can access its value by using DataRowView object. Then after executing your code to customize the value, assign new value to TemplateColumn's TableCell by using its 'UniqueName'.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       int count = 3;
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
           string text = groupDataRow["Status"].ToString();
           item["Status"].Text = text.Substring(0, count);
       }
   }

Thanks,
Princy.
0
ShaneUlker
Top achievements
Rank 1
answered on 22 Oct 2013, 03:45 PM
I want to truncate the text which is in html format. Do you have suggestion for that. Using the below method all html gets messed up inside RadGrid.
0
Princy
Top achievements
Rank 2
answered on 23 Oct 2013, 06:42 AM
Hi ShaneUlker,

Please take a look into the following code snippet i tried to show only the text inside an html tag.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridDataItem)
          {
              GridDataItem item = (GridDataItem)e.Item;
              DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
              string text = groupDataRow["UniqueName"].ToString();
              text=Regex.Replace(text, "<.*?>", string.Empty);
              item["UniqueName"].Text = text;
          }
}

 Let me know if you have any concern.

Thanks,
Princy.
Tags
Grid
Asked by
newbie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
newbie
Top achievements
Rank 1
ShaneUlker
Top achievements
Rank 1
Share this question
or