I have a grid that I open up with all lines editable. I am trying to strip the first 10 characters off of the text in the ItemDataBound event. I have version 2016.3.1027.40.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem)
{
GridEditableItem editableItem = e.Item as GridEditableItem;
//string s = "This Works and is longer than 10 characters";
//if (s.Length > 10)
//{
// s = s.Substring(10);
//}
//editableItem["MasterLineItem"].Text = s;
if (editableItem["MasterLineItem"].Text.Length > 10)
{
editableItem["MasterLineItem"].ToolTip = (editableItem["MasterLineItem"].Text).Substring(10); //THIS DOES NOT WORK
}
}
}
Thanks
4 Answers, 1 is accepted
EDIT: I'm trying to set the Text not the ToolTip.
editableItem["MasterLineItem"].Text = (editableItem["MasterLineItem"].Text).Substring(10); //THIS DOES NOT WORK
I tested the provided functionality but the cell text in the MasterLineItem column gets truncated successfully at my end. Can you see the video from my test and let me know whether this is the value you need to change?
https://www.screencast.com/t/wo00Et6AdQ
You can find the code I used for my test as of below:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"true"
HeaderStyle-Width
=
"150px"
AutoGenerateEditColumn
=
"True"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
>
<
ClientSettings
>
<
Resizing
AllowColumnResize
=
"true"
ResizeGridOnColumnResize
=
"true"
AllowResizeToFit
=
"true"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
(sender
as
RadGrid).DataSource = GetData();
}
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem)
{
GridEditableItem editableItem = e.Item
as
GridEditableItem;
if
(editableItem[
"MasterLineItem"
].Text.Length > 10)
{
editableItem[
"MasterLineItem"
].Text = (editableItem[
"MasterLineItem"
].Text).Substring(10);
}
}
}
private
DataTable GetData()
{
DataTable dt =
new
DataTable(
"data"
);
dt.Columns.Add(
"MasterLineItem"
, Type.GetType(
"System.String"
));
dt.Columns.Add(
"BoundColumn2"
, Type.GetType(
"System.Int32"
));
dt.Columns.Add(
"BoundColumn3"
, Type.GetType(
"System.Int32"
));
for
(
int
i = 0; i < 50; i++)
{
var flag = i % 3 == 0 ?
true
:
false
;
dt.Rows.Add(
"Some long text 1234567890"
+ i.ToString(),
i + 1,
i + 200000
);
}
return
dt;
}
Regards,
Vessy
Telerik by Progress
Vessy,
Thanks for the response. Your video pointed me in the right direction. My grid uses inline edit mode, all rows are in edit mode, and the column I want to truncate is read only. By accessing the Literal of the GridEditableitem and truncating that I am seeing the desired results.
My updated code:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem)
{
GridEditableItem editableItem = e.Item as GridEditableItem;
if (editableItem.IsInEditMode)
{
Literal ltrl = editableItem["MasterLineItem"].Controls[1] as Literal;
if (ltrl.Text.Length > 10)
{
ltrl.Text = ltrl.Text.Substring(10);
}
}
}
}
Regards,
Gerald
I am glad to know my answer was helpful for you and you have managed to achieve the target result. As always, feel free to reach us again should any further questions occur.
Regards,
Vessy
Telerik by Progress