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