Hello,
I have noticed that when clicking "Autofit all columns" from the context menu that the columns are sized correctly to accommodate the contents of each column but this can leave whitespace at the end of the grid:
Is it possible to get the grid to set the column widths slightly wider in order to utilise this white space?
Thank
Hi Monisha,
Thanks for the quick reply. I appreciate that the grid is behaving as documented. However, the problem this causes is that if you have EnablePersistance="true" and you choose the option to Auto Fit All then navigate away and back again, the columns change size.
To reproduce you can use the following grid code:
<SfGrid @ref="DefaultGrid" DataSource="@Orders"
AllowSorting="true"
AllowResizing="true"
ID="TestAutoResize"
EnablePersistence="true"
ContextMenuItems="@(new List<object>() { "AutoFit", "AutoFitAll"})"
>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
private SfGrid<Order> DefaultGrid;
public List<Order> Orders { get; set; }
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 20).Select(x => new Order()
{
OrderID = 1000 + x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
}).ToList();
}
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
}
After Auto Fit All Columns the grid looks like this:
After navigating to Home and back again it looks like this:
I believe this is because on initialising it is trying to make use of all of the space available to it rather than leaving white space. This is what I would like to the Auto Fit All Columns to do so that as the user navigates back to the grid the columns are still sized as they were previously.
I hope that is clear.
Thanks,
Chuck
Hi Monisha,
172610 is related but is a different issue. in 172610 I have asked how to get the AutoFit All behaviour to use the white space.
While this form entry 172611 is about the fact that when you use AutoFit All with EnablePersistence=true and navigate away from the grid and back again, it does not set the column widths to what they were after using AutoFit All.
I hope the distinction between the two forum entries is clear.
Thanks,
Chuck
Hi Monisha,
Thanks for logging the use of white space as a potential future enhancement.
In the meantime, how can I get the grid to size the columns the way it does when it renders the first time? In other words, if a user changes grid column sizes and then wants to revert back to how the grid displayed when they first landed on the page, how I can I achieve this?
Thanks,
Chuck
|
<SfButton @onclick="Refresh">Reset</SfButton>
@if (canRender)
{
<SfGrid ID="GridSizeNew" @ref="Grid" DataSource="@Orders" AllowResizing="true" AllowPaging="true" ContextMenuItems="@(new List<object>() { "AutoFit", "AutoFitAll"})" AllowGrouping="true" AllowReordering="true">
<GridColumns>
...
</GridColumns>
</SfGrid>
}
@code{
public bool canRender = true;
public async Task Refresh()
{
if (canRender)
{
canRender = false;
}
else
{
canRender = true;
}
}
} |