Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="listView"></param>
        public static void AutoSizeColumns(this RadListView listView)
        {
            using (var defer = listView.DeferRefresh())
            {
                var sizes = listView
                            .Items
                            .Select(i =>
                {
                    var cols = new List <int>();
                    for (var m = 0; m < i.FieldCount; ++m)
                    {
                        object value = i[m];
                        if (value == null)
                        {
                            value = "";
                        }
                        cols.Add(TextRenderer.MeasureText(value.ToString(), listView.Font).Width + 4);
                    }
                    return(cols.ToArray());
                })
                            .ToArray();

                for (var c = 0; c < listView.Columns.Count; ++c)
                {
                    var col = listView.Columns[c];
                    if (!col.Visible)
                    {
                        continue;
                    }

                    if (sizes.Length == 0)
                    {
                        col.Width = 1;
                    }
                    else
                    {
                        col.Width = Math.Max(1, sizes.Max(s => s[c]));
                    }
                }
            }
        }