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

Resizing Columns Programmatically

8 Answers 267 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 2
Sergey asked on 11 Feb 2021, 03:05 PM
Hello Telerik, can I resizing columns programmatically, as in winforms?

8 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 16 Feb 2021, 09:11 AM

Hello Sergey,

There is no direct method for resizing columns individually, but the control provides APIs that can be useful in your case. The ColumnWidth property of RadVirtualGrid allows you to set an unified size for all columns. Also, you can use the FitColumnWidthToContent method which resizes the column's width to the size of the largest cell. Additionally, there is an HeaderSizeNeeded event which fires when the control is measured and arranged. In the event handler, you can set the size of the header. For example:

public MainWindow()
{
	InitializeComponent();
	this.virtualGrid.HeaderSizeNeeded += RadVirtualGrid_HeaderSizeNeeded;
}

private void OnButtonClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
	SetColumnWidth(1, 20);
}

private double columnWidthCache = -1;
private double columnIndexCache = -1;

private void SetColumnWidth(int columnIndex, double width)
{
	this.columnIndexCache = columnIndex;
	this.columnWidthCache = width;

	var widthCache = this.virtualGrid.ColumnWidth;
	this.virtualGrid.ColumnWidth = 0;
	this.virtualGrid.ColumnWidth = widthCache;
}

private void RadVirtualGrid_HeaderSizeNeeded(object sender, Telerik.Windows.Controls.VirtualGrid.HeaderSizeEventArgs e)
{   
	if (e.Index == this.columnIndexCache)
	{
		e.Size= this.columnWidthCache;
	}
}

I hope this information helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Sergey
Top achievements
Rank 2
answered on 16 Feb 2021, 09:58 AM

Hello Martin, 

Thanks for the example, this looks like what I need.

0
Sergey
Top achievements
Rank 2
answered on 16 Feb 2021, 12:39 PM
I tried an example. For some reason, HeaderSizeEventArgs always contains the same default value (in my case, it is 150 pixels) for the size, although it is already different. Why is this happening and is it possible to somehow get the current real size of the header?
0
Sergey
Top achievements
Rank 2
answered on 16 Feb 2021, 12:42 PM
And there is still no way to separate RowHeader from ColumnHeader. It might be worth adding to the event.
0
Vladimir Stoyanov
Telerik team
answered on 19 Feb 2021, 08:28 AM

Hello Sergey,

Currently there isn't an option to obtain the actual size of the header. That said, if you are modifying the sizes in code, you can keep them in a variable (for example a dictionary of column index - size). Additionally, you can use the HeaderOrientation property of the event arguments to determine whether the event is fired for a row or column header. That said, currently it is only fired for column headers.

On a side note, we have logged a new feature request for introducing an api for resizing the columns in code: VirtualGrid: Introduce API for programatically resizing the columns. You can follow the item in order to get notified for any updates. I have also added some telerik points to your account as a thank you for bringing this to our attention. 

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Sergey
Top achievements
Rank 2
answered on 22 Feb 2021, 07:50 AM

Hello Vladimir,

At the moment, I still save the dimensions, but the user can change them and this data is already lost without the ability to get the size programmatically. With regards to HeaderOrientation, it was meant that the event is called 2 times with the HeaderOrientation Horizontal and 0 column index. And it is not possible to understand whether the column is mine or the system one for the row headers.

That is, my task is to save and load user settings, and at the moment this is impossible.

0
Accepted
Vladimir Stoyanov
Telerik team
answered on 24 Feb 2021, 04:10 PM

Hello Sergey,

Thank you for the update. 

I am afraid that the RadVirtualGrid does not support obtaining the actual size of the columns after the user has resized them. This is due to the fact that the control has very few actual visual elements in order to improve its performance. However, this comes at the cost of lacking certain features. 

If obtaining the size of the columns at runtime is important for your scenario, I can suggest switching to the RadGridView, which is the feature-rich alternative of the RadVirtualGrid. 

On a side note, the HeaderSizeNeeded event is fired for the user columns first and for the row headers last. With this in mind, the first time that the index is 0, the event is fired for the first column and the second time, it is fired for the row indicator column. 

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Sergey
Top achievements
Rank 2
answered on 25 Feb 2021, 06:55 AM

Hello Vladimir,

Thanks for the info.

Regarding RadGridView, I am aware, VirtualGrid was chosen because performance is required, however, some functions are still missing, but there is no way to sacrifice performance to get them.

Tags
VirtualGrid
Asked by
Sergey
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Sergey
Top achievements
Rank 2
Vladimir Stoyanov
Telerik team
Share this question
or