@using Syncfusion.Blazor.Grids
<SfGrid @ref="grd" DataSource="@Orders" AllowPaging="true">
<GridPageSettings PageSize="5"></GridPageSettings>
<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{
SfGrid<Order> grd;
. . .
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (grd.Columns != null)
{
((List<GridColumn>)grd.Columns).RemoveAt(2);
grd.RefreshColumns();
grd.RefreshHeader();
grd.Refresh();
}
return Task.FromResult(0);
}
} |
. . .
<SfGrid @ref="DefaultGrid" DataSource="@Orders" Height="315">
<GridEvents DataBound="DataBoundHandler" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Center" Width="150"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150" TextAlign="TextAlign.Center"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" Visible="false" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Center" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Center" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
public string[] ColumnItems = new string[] { "Freight" };
. . .
public void DataBoundHandler()
{
// Hide columns by its header text
this.DefaultGrid.HideColumns(ColumnItems);
}
} |
@page "/"
@using Syncfusion.Blazor.Grids
<SfGrid @ref="@Grid" Columns="@Cols" ID="Egrid" DataSource="@Orders" AllowFiltering="true" AllowPaging="true" Height="315">
</SfGrid>
@code{
SfGrid<Order> Grid;
public List<Order> Orders { get; set; }
List<GridColumn> Cols = new List<GridColumn>(); //columns bounded to Grid.
protected override void OnInitialized()
{
//initially defined 4 columns
Cols.Add(new GridColumn() { Field = "OrderID", HeaderText = "ID", Width = "75" });
Cols.Add(new GridColumn() { Field = "CustomerID", HeaderText = "Name", Width = "200", Filter = new { @operator = "contains" } });
Cols.Add(new GridColumn() { Field = "Freight", HeaderText = "H Text", Width = "100" });
Cols.Add(new GridColumn() { Field = "OrderDate", HeaderText = "Date", Width = "100" });
Orders = Enumerable.Range(1, 75).Select(x => new Order()
{
. . .,
}).ToList();
}
protected override Task OnAfterRenderAsync(bool firstRender)
{
Cols = new List<GridColumn>(); //redefine the colums. //defined only two columns
Cols.Add(new GridColumn() { Field = "OrderID", HeaderText = "ID", Width = "75" });
Cols.Add(new GridColumn() { Field = "CustomerID", HeaderText = "Name", Width = "200", Filter = new { @operator = "contains" } });
Grid.Columns = Cols; //assigned new columns to Grid columns
return Task.FromResult(0);
}
} |
This code generates a BL0005 Error.
We are facing the same problem. We have many columns for the users to choose from, but by including them with "Visible=False" we are having performance issues caused by the DOM including the non-visible columns.
Instead, we would like to be able to dynamically create the columns based on customer settings. This solution is almost there, but you are not allowed to create a GridColumn in code because of the BL0005 error.
Do you have any other possible solutions?
protected override void OnInitialized()
{
#pragma warning disable BL0005 // you can resolve the warning by using this
Cols.Add(new GridColumn() { Field = "OrderID", HeaderText = "ID", Width = "75" });
Cols.Add(new GridColumn() { Field = "CustomerID", HeaderText = "Name", Width = "200" });
Cols.Add(new GridColumn() { Field = "Freight", HeaderText = "H Text", Width = "100" });
Cols.Add(new GridColumn() { Field = "OrderDate", HeaderText = "Date", Width = "100" });
#pragma warning restore BL0005 // you can resolve the warning by using this
. . .
} |
Hi,
How may I add or remove a column by different screen size . for example show a column if screen is bigger than md.
thanks
Hi Madjid,
Query:” How may I add or remove a column by different screen size”
We recommend using MediaQuery component to dynamically adjust column visibility
in the grid based on different screen sizes. Please refer to the code snippet
and sample below for reference.
<SfMediaQuery @bind-ActiveBreakpoint="@activeBreakpoint"></SfMediaQuery>
<SfGrid @ref="@Grid" Columns="@Cols" ID="grid" DataSource="@Orders" AllowFiltering="true" AllowPaging="true" Height="315"> </SfGrid>
@if (activeBreakpoint == "Large") { AddandRemoveColumn(); } else if (activeBreakpoint == "Small") { AddandRemoveColumn(); }
@code {
private string activeBreakpoint;
SfGrid<Order> Grid; public List<Order> Orders { get; set; } List<GridColumn> Cols = new List<GridColumn>(); //columns bounded to Grid.
public void AddandRemoveColumn() { Cols.RemoveAt(1); Cols.Add(new GridColumn() { Field = "OrderDate", HeaderText = "Date", Width = "100" }); Grid.Refresh();
}
} |
Reference:
https://blazor.syncfusion.com/documentation/media-query/break-points
Regards,
Prathap S