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

Column Alignment and Width

7 Answers 249 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 08 Sep 2019, 09:30 AM

Hi,

I'm having trouble applying both FitColumnWidthToContent and CellTextAlignment.

Re: FitColumnWidthToContent , I'm using a custom data provider. It obtains its data through a 2-step process. First, to help clean up memory (the grid is being used essentially as a pivot table and so it gets heavy use), the existing collection is cleared and the associated custom data property is nullified prior to each load:

CustomData?.Clear();
CustomDataProvider = null;

Then the data is reloaded via CustomData = PivotsDB.RetrieveData();

Both CustomData and CustomDataProvider are properties that call RaisePropertyChanged() as per your ViewModelBase helper class.

As a consequence of this setup, the Loaded event for my RadVirtualGrid only fires once, i.e. the fist time the grid is loaded.

To implement your suggested code re: FitcolumnWidthToContent, I therefore created a private method:

private void DataChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("CustomData"))
                return;
            for (int i = 0; i < RadVirtualGrid1.InitialColumnCount; i++)
            {
                RadVirtualGrid1.FitColumnWidthToContent(i);
            }
        }

...and registered this method in the code-behind file:

            InitializeComponent();
            var mvm = Application.Current.MainWindow.DataContext as MainWindowViewModel;
            PivotsViewModel vm = mvm.CurrentPageViewModel as PivotsViewModel;
            vm.PropertyChanged += DataChanged;

I have confirmed that my DataChanged method is firing, but it has no effect on the column sizes. And, yes, I have set my MeasureTextOnRender="True".

The problem with CellTextAlignment is much more straightforward. I'm using your recommended code as follows:

private void RadVirtualGrid1_CellDecorationsNeeded(object sender, Telerik.Windows.Controls.VirtualGrid.CellDecorationEventArgs e)
        {
            e.CellTextAlignment = TextAlignment.Right;
        }

...but CellTextAlignment is not a member of CellDecorationEventArgs, and if it is an extension method, VS isn't helping me find it.

Wayne

7 Answers, 1 is accepted

Sort by
0
n/a
Top achievements
Rank 1
answered on 08 Sep 2019, 10:19 AM

Actually, forget most of that preceding message, as I figured it out. Not only were there code errors on my part, but also a timing issue that I fixed by listening for a different property change.

However, I'm still left with one small issue. The columns are now resizing, but they are doing so based on the width of the header values, not the cell values. I've attached a picture.

Also, my problem with CellTextAlignment remains.

Wayne

0
n/a
Top achievements
Rank 1
answered on 08 Sep 2019, 01:31 PM

Apologies for subjecting you all to my conversation with myself!

I have found a solution re: FitColumnWidthToContent as it is, though I certainly welcome better suggestions from Telerik.

I build my pivot table dynamically anyways, so I decided to just store a desired column width along with the field definition data.

Then I calculated the additional characters required to achieve my desired width by subtracting the header length from the target width:

additionalChars = targetWidth - Breakdowns[e.Index].Length;
if (additionalChars < 0) { additionalChars = 0;}

Finally, I modified the column headers by tacking on additional invisible characters:

e.Value = Breakdowns[e.Index] + new string('\u00A0', additionalChars);

Feels like a hack, but it does the trick, and that's all I could come up with on my own.

Wayne

0
Dilyan Traykov
Telerik team
answered on 11 Sep 2019, 10:44 AM

Hi Wayne,

I want to start with clarifying that the CellTextAlignment property is available since the 2019.2.729 internal build of the controls. You can have a look at the following feature request for more information. Can you please clarify whether you're using this version of the suite or newer?

As for the use of the FitColumnWidthToContent method, I tried using it after updating the provider's source and this seems to work as expected at my end. I've attached a small sample project as a demonstration.

Please have a look and let me know how this approach differs from the one at your end.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
n/a
Top achievements
Rank 1
answered on 12 Sep 2019, 12:44 AM

Hi Dilyan,

Are you sure that you sent me the write files? I ask because there is no reference in that project to FitColumnWidthToContent, and all the columns are standard width.

On the bright side, I was able to fix my column alignment using the latest internal build.

Wayne

0
Dilyan Traykov
Telerik team
answered on 12 Sep 2019, 06:50 AM

Hi Wayne,

Please excuse me, I seem to have uploaded the wrong archive. You can find attached the intended project files.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
n/a
Top achievements
Rank 1
answered on 14 Sep 2019, 03:14 AM

No problem. I've looked at your update files and understand how everything is intended to work. However, it does not work that way for me. I've tested it numerous times, and my call to FitColumnWidthToContent is definitely sizing to the header. Proof of this is the fact that the cell content is being truncated to the width of the header.

I think this may be happening because I call FitColumnWidthToContent in response to a custom event, at which point the cell contents may not yet be established but the cell header contents are. Having said that, my solution turned out to be the one I wanted because there are some anomalously long descriptions in the cell content, and I don't want to size data grid columns based on anomalously long descriptions when 99 % of the cell content is considerably shorter.

In other words, thanks for the clarification but you can now mark this issue as resolved.

For others following this thread, in case you ever find yourself with similar goals, I'm using RadVirtualGrid as a specialized pivot table where columns are dynamically created with the help of DB metadata. Because I wanted absolute control over column widths, I added a desired column width to the DB metadata and use that to size my column headers. Because of the way I trigger FitColumnWidthToContent, the sizes of my column headers are dictating the column width. This is what's called an "accidental solution" :).

 

 

0
Dilyan Traykov
Telerik team
answered on 16 Sep 2019, 07:28 AM

Hi Wayne,

I'm happy to hear that you've found a suitable solution for your particular case. I'd also like to thank you for sharing your approach with our community.

Should any other issues or concerns arise, do not hesitate to contact me again.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
VirtualGrid
Asked by
n/a
Top achievements
Rank 1
Answers by
n/a
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or