sfDataGrid with List of Lists

Good morning, excuse me, how could I load a sfDataGrid with a list of lists? I do not use MVVM. I have the following

var head= new List();
var body = new List();
var completo = new List>();

//Fixed headboard
head.Add("Column1");
head.Add("Column2");
head.Add("Column3");

/Extra columns

for(var i = 4; i < arreglo.Count; i++){
     if(the condition is met){
           head.Add("Column" + i);
     }
}

completo.add(head)

body.add("item1.1","1.2",1.3","1.4"...);
completo.add(body);
body.add("item2.1","2.2",2.3","2.4"...);
completo.add(body);
body.add("item3.1","3.2",3.3","3.4"...);
completo.add(body);


sfDatagrid.ItemSource = completo????

the idea is to fill the datagrid with the list of lists and finish like this

Column1 Column2 Column3 Column4 ....
   item1.1   item1,2   item1.3    item1.4 ....
   item2.1   item2,2   item2.3    item2.4 ....
   item3.1   item3,2   item3.3    item3.4 ....
   item4.1   item4,2   item4.3    item4.4 ....


but what i get is this, please, is there a way to fill the sfDataGrid with a list of lists??














3 Replies

KK Karthikraja Kalaimani Syncfusion Team August 25, 2020 12:54 PM UTC

Hi David,

Your requirement can be achieved with single list. Please refer to the below code snippet and attached sample. Please let us know why did you maintaining the list for Column Header and Column as separate list ?

Code snippet :

 
<syncfusion:SfDataGrid x:Name="myGrid"  
                               AllowEditing="True"  
                               NavigationMode="Cell" 
                               ItemsSource="{Binding Completo}" 
                               SelectionMode="Single"  
                               ColumnSizer="Star" 
                               AutoGenerateColumns="False"> 
            <syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTextColumn HeaderText="Column1" MappingName="Item1"></syncfusion:GridTextColumn> 
                <syncfusion:GridTextColumn HeaderText="Column2" MappingName="Item2"></syncfusion:GridTextColumn> 
                <syncfusion:GridTextColumn HeaderText="Column3" MappingName="Item3"></syncfusion:GridTextColumn> 
                <syncfusion:GridTextColumn HeaderText="Column4" MappingName="Item4"></syncfusion:GridTextColumn> 
            </syncfusion:SfDataGrid.Columns> 
        </syncfusion:SfDataGrid>

 
//ViewModel
private List<BodyItems> Body { get; set; }
 
 
        public List<BodyItems> Completo { get; set; } 
 
        public Datas() 
        { 
            Body = new List<BodyItems>(); 
            Completo = new List<BodyItems>(); 
            var bodyItems = new BodyItems("item1.1", "1.2", "1.3", "1.4"); 
            Body.Add(bodyItems); 
            var bodyItems2 = new BodyItems("item2.1", "2.2", "2.3", "2.4"); 
            Body.Add(bodyItems2); 
            var bodyItems3 = new BodyItems("item3.1", "3.2", "3.3", "3.4"); 
            Body.Add(bodyItems3); 
            Completo = Body; 
                      ; 
            mycollect = new ObservableCollection<Data>(); 
            mycollect.Add(new Data { No_1 = 10, No_2 = 20, Text = "MyAverage" }); 
        }
//Model
public class BodyItems
 
    { 
        public string Item1 { get; set; }  
 
        public string Item2 { get; set; } 
 
        public string Item3 { get; set; } 
 
        public string Item4 { get; set; } 
        public BodyItems(string item1,string item2, string item3, string item4) 
        { 
            Item1 = item1; 
            Item2 = item2; 
            Item3 = item3; 
            Item4 = item4; 
        } 
    } 
  
Screenshot :

 
 



DA David August 25, 2020 01:00 PM UTC

Thanks for the prompt reply, the downside is that the columns are not fixed, and they are not always the same. sometimes they can be 4 or 8 and they can all be different.


KK Karthikraja Kalaimani Syncfusion Team August 26, 2020 01:34 PM UTC

Hi David,

Please provide screenshot or video about the issues “downside is that columns are not fixed” with Complete SfDataGrid configuration and model and view model codes. It will help us to provide you a better solution earlier. Please let us know that the previous update by our side meets your requirement?

Regards,
Karthik Raja 


Loader.
Up arrow icon