0
votes

I am adding a dynamic column to an ASP.NET grid view.

Code to add the dynamic column:

List<DataControlField> columns; // this contains all gridview columns. 
BoundField boundField = new BoundField();
boundField.DataField = long_text_column.SortExpression;
boundField.SortExpression = long_text_column.SortExpression;
columns.Insert(0, boundField);

How can I trim / truncate long_text_column to show only first 15 characters on the UI.

NOTE: I do not want to trim at the database level for other reasons.

1
What I would do would be to use the RowDataBound event, validate if it is type DataControlRowType.DataRow and make Substring (0, 15) to the cell - Julián
I think what Julian has said or you could try some CSS maybe. - Salik Rafiq

1 Answers

0
votes

I would add an extra property to the class with only a get that returns long_text_column with a max length of 15.

public class DataControlField
{
    public string long_text_column { get; set; }

    public string long_text_column_max15
    {
        get
        {
            if (!string.IsNullOrEmpty(long_text_column) && long_text_column.Length > 15)
                return long_text_column.Substring(0, 15);
            else
                return long_text_column;
        }
    }
}