Viewmodel for gridview with c # column headers

I need a solution for this.

I need to create a view model to represent the grid. This model must be strong typed. ex.

List<Person> lstPersons=new List<Person>(); 

something like that. Also, with this I have to have custom column header names. I can go with data annotation with AutoGenerateColumns="True" turned on AutoGenerateColumns="True" like

 class Person { [DisplayName("Person Name")] public string name { get; set; } } 

something like that. But I have 2 questions.

  • I do not know how to change this display name at runtime.

  • I am using telerik RADGridView. with this, when I use AutoGenerateColumns="True" and ShowColumnFooters="True" , the whole user interface gets stuck. I think this is a mistake with telerik control. Therefore, I have to define all the columns in XAML and add a binding path for each.

In any case, this is possible with a DataTable. but I believe that data tables are a very complex structure and a heavy object.

How to create a viewmodel for this? Any suggestions?

Feel free to ask any questions. im not sure the above description is available to everyone.

+4
source share
3 answers

You can continue to use the DisplayName attribute on your model, but, as you indicated, cannot change it at run time. To do this, use the dictionary in the ViewModel, which populates

 public PeopleGridViewModel { public ObservableCollection<Person> People; public Dictionary<string, string> PersonColumnHeaders; public PeopleGridViewModel() { // 1. write C# here to snag the property names from the typeof(Person) // 2. get the DisplayName attribute value for that property too. // 3. add the propertyName.ToString() and the DisplayName string to the dictionary as a key/value pair. // the result is you have a collection of column headers that start with the defaults from the propertys on the object... but they can be manipulated at run-time, and you don't havem them 100% hard typed like in adcool example. } } 

I think the footers in the columns of the show are a Telerik issue, as you suggested.

+1
source

Try a discussion about binding data to a column heading.

+1
source

I assume that the columns you want to display are Fixed .... so your ViewModel will look like

  class MyViewModel { //Implement Properties nad proper binding in Xaml and INotifyPropertyChanged for every property that you need on View Level ObservableCollection<Persons> persons; string columnheader1; string columnheader2; string columnheader3; } 

Xaml

  <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding UserName}" Width="200"> <telerik:GridViewDataColumn.Header> <TextBlock Text="{Binding Columnheader1}"></TextBlock> </telerik:GridViewDataColumn.Header> </telerik:GridViewDataColumn> <telerik:GridViewDataColumn Header="{Binding Columnheader2}" DataMemberBinding="{Binding IsOnline}" MinWidth="200" Width="*"/> <telerik:GridViewDataColumn Header="{Binding Columnheader3}" DataMemberBinding="{Binding LastActivityDate}" MinWidth="200"/> </telerik:RadGridView.Columns> 

This can do the trick ..... :)

+1
source

Source: https://habr.com/ru/post/1384645/


All Articles