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

SetColumnWidth

2 Answers 62 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 2
Iron
Iron
Veteran
Dave asked on 19 Jul 2020, 06:54 PM

I've been playing with the 2020 R1 demo and am having trouble setting and getting column widths of the VirtualGrid.

To set the column widths I've tried both TableElement, as in the documentation, and MasterViewInfo, which I found in the forums.

A little insight would be great!

 

Thanks!

using System;
using System.Collections.Generic;
using Telerik.WinControls.UI;
 
namespace TelerikWinFormsApp1
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        List<Foo> data = new List<Foo>();
 
        public RadForm1()
        {
            InitializeComponent();
            this.Text = "Foo";
 
            radVirtualGrid1.AutoSize = false;
            this.radVirtualGrid1.CellValueNeeded += new Telerik.WinControls.UI.VirtualGridCellValueNeededEventHandler(this.radVirtualGrid1_CellValueNeeded);
            this.radVirtualGrid1.ColumnWidthChanged += new Telerik.WinControls.UI.VirtualGridColumnEventHandler(this.radVirtualGrid1_ColumnWidthChanged);
 
            //These methods do not set the column width
            radVirtualGrid1.MasterViewInfo.SetColumnWidth(0, 300);
            radVirtualGrid1.MasterViewInfo.SetColumnWidth(1, 150);
            radVirtualGrid1.TableElement.SetColumnWidth(0, 300);
            radVirtualGrid1.TableElement.SetColumnWidth(1, 150);
 
            LoadData();
        }
 
        private void radVirtualGrid1_ColumnWidthChanged(object sender, VirtualGridColumnEventArgs e)
        {
            //This is always 100
            int i = e.ViewInfo.ColumnWidth;
        }
 
        private void radVirtualGrid1_CellValueNeeded(object sender, Telerik.WinControls.UI.VirtualGridCellValueNeededEventArgs e)
        {
            if (e.ViewInfo == this.radVirtualGrid1.MasterViewInfo)
            {
                if (e.ColumnIndex < 0)
                {
                    return;
                }
 
                e.FieldName = Foo.FieldNames[e.ColumnIndex];
 
                if (e.RowIndex == RadVirtualGrid.HeaderRowIndex)
                {
                    e.Value = e.FieldName;
                }
                else if (e.RowIndex >= 0)
                {
                    e.Value = data[e.RowIndex][e.ColumnIndex];
                }
            }
        }
 
        private void LoadData()
        {
            Random random = new Random();
 
            for (int i = 0; i < 10; i++)
            {
                Foo Foo = new Foo();
                Foo.Me = i;
                Foo.Yu = i;
                data.Add(Foo);
            }
 
            this.radVirtualGrid1.RowCount = data.Count;
            this.radVirtualGrid1.ColumnCount = Foo.FieldNames.Length;
        }
    }
 
    public class Foo
    {
        public static readonly string[] FieldNames = { "Me", "Yu" };
        public int Me { get; set; }
        public int Yu { get; set; }
 
        public int this[int index]
        {
            get
            {
                switch (index)
                {
                    case 0: return Me;
                    case 1: return Yu;
                    default: return 0;
                }
            }
        }
    }

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Jul 2020, 09:55 AM

Hello, David, 

Following the provided code snippet, I have prepared a sample project to test the behavior on my end. 

I have noticed that the virtual grid is populated with columns and data after you call the SetColumnWidth methods. At this point there are no columns in RadVirtualGrid. That is why the columns are not properly resized.

If you call the LoadData method prior setting the columns' width, you will observe the expected result:

I have attached my sample project for your reference.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

0
Dave
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 20 Jul 2020, 02:26 PM
So simple.  Thanks!
Tags
VirtualGrid
Asked by
Dave
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Dave
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or