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

Automatic column resize depending on content length

1 Answer 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris Gårdenberg
Top achievements
Rank 2
Chris Gårdenberg asked on 26 Mar 2010, 08:48 AM
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 
                             { 
                                 cuss.CompetitorID, 
                                 cuss.companyName, 
                                 cuss.countryRegistrationNumber, 
                                 cuss.emailAdress, 
                                 cuss.homepageUrl, 
                                 cuss.headquarterOrBranch, 
                                 cuss.legalFormCode, 
                                 cuss.created, 
                                 cuss.updated, 
                                 cuss.branchesQuantity, 
                                 cuss.OwnerEntity, 
                                 Notes = cuss.OwnerEntity.EntityNotes.Count(), 
                                 HasTheLatestNote = (theVeryLatestNote != null ? (cuss.OwnerEntity.EntityID == theVeryLatestNote.OwnerEntity.EntityID) : false
                             }; 
 
            DataTable dt = new DataTable(); 
            dt.Columns.Clear(); 
            dt.Columns.Add("CompetitorID"typeof(int)); 
            dt.Columns.Add("companyName"typeof(string)); 
            dt.Columns.Add("countryRegistrationNumber"typeof(string)); 
            dt.Columns.Add("emailAdress"typeof(string)); 
            dt.Columns.Add("homepageUrl"typeof(string)); 
            dt.Columns.Add("headquarterOrBranch"typeof(string)); 
            dt.Columns.Add("legalFormCode"typeof(string)); 
            dt.Columns.Add("created"typeof(DateTime)); 
            dt.Columns.Add("updated"typeof(DateTime)); 
            dt.Columns.Add("branchesQuantity"typeof(int)); 
            dt.Columns.Add("Notes"typeof(int)); 
            dt.Columns.Add("HasTheLatestNote"typeof(bool)); 
 
            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}; 
                    grdCI2.MasterTableView.Columns.Add(col); 
                } 
            } 
            else 
                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; 
                    } 
                } 
                dt.Rows.Add(dr); 
            } 
 
            return dt; 
        } 

1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 01 Apr 2010, 06:29 AM
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:

http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

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

Regards,
Yavor
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.
Tags
Grid
Asked by
Chris Gårdenberg
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Share this question
or