Automatic column resize depending on content length

2 posts, 1 answers
  1. Chris Gårdenberg
    Chris Gårdenberg avatar
    13 posts
    Member since:
    Nov 2008

    Posted 26 Mar 2010 Link to this post

    Hi, I would like to know if it is possible to resize a column depending the length on the content.

    For example, I have a product that uses several dynamic fields with contents ranging from 1 character up to (think free-text).

    Here's the code I use to generate all fields:

            DataTable createDataTableWithCustomFields() 
                NBKCustomer[] custs = c.GetCompetitors(AuthToken); 
                var theVeryLatestNote = custs.SelectMany(cu => cu.OwnerEntity.EntityNotes).OrderByDescending(n => n.Created).FirstOrDefault(); 
                var IamEskimoe = from cuss in custs 
                                 select new 
                                     Notes = cuss.OwnerEntity.EntityNotes.Count(), 
                                     HasTheLatestNote = (theVeryLatestNote != null ? (cuss.OwnerEntity.EntityID == theVeryLatestNote.OwnerEntity.EntityID) : false
                DataTable dt = new DataTable(); 
                IEnumerable<CustomDataField[]> cols; 
                if (IamEskimoe.Any(i => i.OwnerEntity != null)) 
                    cols = (from i in IamEskimoe 
                            where i.OwnerEntity.CustomDataFields.Count() > 0 
                            select i.OwnerEntity.CustomDataFields); 
                    foreach (CustomDataField field in cols.SelectMany(f => f).Where(field => !dt.Columns.Contains(field.CustomDataFieldName))) 
                        dt.Columns.Add(field.CustomDataFieldName, typeof (string)); 
                        GridBoundColumn col = new GridBoundColumn {HeaderText = field.CustomDataFieldName, AllowFiltering = false, DataField = field.CustomDataFieldName}; 
                    cols = null
                foreach (var n in IamEskimoe) 
                    DataRow dr = dt.NewRow(); 
                    dr["CompetitorID"] = n.CompetitorID; 
                    dr["companyName"] = n.companyName; 
                    dr["countryRegistrationNumber"] = n.countryRegistrationNumber; 
                    dr["emailAdress"] = n.emailAdress; 
                    dr["emailAdress"] = n.homepageUrl; 
                    dr["headquarterOrBranch"] = n.headquarterOrBranch; 
                    dr["legalFormCode"] = n.legalFormCode; 
                    dr["created"] = n.created; 
                    dr["updated"] = n.updated; 
                    dr["branchesQuantity"] = n.branchesQuantity ?? (object)DBNull.Value; 
                    dr["Notes"] = n.Notes; 
                    dr["HasTheLatestNote"] = n.HasTheLatestNote; 
                    if (cols != null
                        var n1 = n; 
                        foreach (CustomDataField field in cols.SelectMany(f => f).Where(r => r.FieldData != null && r.FieldData.OwnerEntity.EntityID == n1.OwnerEntity.EntityID)) 
                            dr[field.CustomDataFieldName] = field.FieldData.Data ?? (object)DBNull.Value; 
                return dt; 

  2. Answer
    Yavor avatar
    11 posts

    Posted 01 Apr 2010 Link to this post

    Hi Chris,

    One possible option in this case would be to iterate through all the items in the grid (RadGrid1.MasterTableView.Items), access the relevant cells:

    and based on the contents, set the width of the column(s).
    I hope this suggestion helps.

    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top