Re-Order DataGrid Columns

I want to dynamically set columns and the order of columns based on a user settings for the RadzenDataGrid component. Is this possible for me to set in code?

I just saw the Visible property on a column. This is perfect! A follow up question I have around this same topic is whether I can set the index of a given column. For example, let's say I want my users to be able to not only save settings on what columns they want to be visible, but also in what order they want those columns to be in.

You cannot set the index of a column however you can define the columns in the order specified by your users. Something like:

foreach (var column in columnSettings.OrderBy(c => c.Index))
{
  <RadzenDataGridColumn TItem="MyItem" Property=@column.Property Visible=@column.Visible />
}

Interesting. Does this mean that I have to define my columns in code, then use this iterating in the component?

Also, where are you getting columnSettings?

Yes, you need to store the settings of your columns in a page property e.g. columnSettings. You can create a helper class for that purpose:

class ColumnSettings
{
   public string Property { get; set; }
   public int Index { get; set; }
   public bool Visible { get; set; }
}

IEnumerable<ColumnSettings> columnSettings = new [] { 
   new ColumnSettings { Index = 0, Property = "FirstName" , Visible = true }, 
   new ColumnSettings { Index = 1, Property = "LastName", Visible = false } 
];
2 Likes

This is not a great solution. It assumes each datagridcolumn is of a standard format. Doesn't really work when, for example, you have a templated column. You need to do a big list of ifs, which is a big mess. Can we not have a parameter on the column for Index to allow it to be set? Other blazor grids have the ability to set this at runtime (e.g. DevExpress grid can take the layout parameters at load time and sets the column order).

It's a bit of a waste having a grid with the ability to reorder the columns, but no ability to save that ordering for the user.

Hi @elyl,

You can open a feature request here. We would also gladly accept a pull request.