Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bind a List<Object> which contains List<String> to DataGrid

Tags:

c#

wpf

datagrid

My question is how to bind a List<Object> ( Which contains a List<String> ) to a DataGrid in WPF

Lets say the Class looks like this

Class Student
{
         String Name;
         List<String> Marks;
}

Lets say i have List<Student> but how would i bind this to a Data Grid in the Data Source.

I am sure this, DataGrid.DataSource=List<Student> wouldn't work as expected.

This is how i want

enter image description here

like image 322
Usual Suspect Avatar asked Jun 23 '14 06:06

Usual Suspect


4 Answers

here you go

    <DataGrid ItemsSource="{Binding Students}"
              AutoGenerateColumns="False"
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"
                                Header="Name" />
            <DataGridTextColumn Binding="{Binding Marks[0]}"
                                Header="Mark1" />
            <DataGridTextColumn Binding="{Binding Marks[1]}"
                                Header="Mark2" />
            <DataGridTextColumn Binding="{Binding Marks[2]}"
                                Header="Mark3" />
        </DataGrid.Columns>
    </DataGrid>

also change class as

    class Student
    {
        public String Name { get; set; }
        public List<String> Marks { get; set; }
    }

note I made public properties for your variables

result

sample

Variable Columns

you can have variable number of columns but not for each row

method1 hard-code maximum columns in xaml, so if a column does not have a value for that row it will remain empty

eg

grid sample 2

I have added another column to demonstrate clearly

<DataGridTextColumn Binding="{Binding Marks[3]}"
                    Header="Mark4" />

other approach involve to generate columns at run-time via code behind or via help of attached properties

like image 142
pushpraj Avatar answered Oct 06 '22 18:10

pushpraj


you can try like this

<DataGrid ItemsSource="{Binding lstStu}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="name" Binding="{Binding Name}"/>
                <DataGridTemplateColumn Header="list">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListBox ItemsSource="{Binding Marks}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

if you don't want to display mark in list and you have fixed number of mark,you can do it like this

<DataGridTextColumn Header="name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="mark1" Binding="{Binding Marks[0]}"/>
<DataGridTextColumn Header="mark2" Binding="{Binding Marks[1]}"/>
like image 35
Rang Avatar answered Oct 06 '22 20:10

Rang


In the first place you need to have public properties instead of fields in your model. Like that:

public class Student
{
         public String Name {get;set;}
         public List<String> Marks {get;set;}
}

Then provide datacontext for the grid. In code behind add something like that:

var StudentsList = new List<Student>();
//populate the list.....    
DataContext=StudentsList;

The last thing is to bind the DataContext to controls in xaml. Like in the other answers:

 <ListView ItemsSource="{Binding StudentsList}">
        <ListView.ItemTemplate>
                <DataTemplate>
                       <DataGrid ItemsSource="{Binding Marks}" />
                </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>
like image 1
klm_ Avatar answered Oct 06 '22 19:10

klm_


you have to use below menioned code

Class Student
{
         String Name;
         List<String> Marks;
}


 private ObservableCollection<Student> _student=new ObservableCollection<Student>();

    public ObservableCollection<Student> student
    {
        get { return _student; }
        set { _student = value; }
    }

and your Itemsource look like

<DataGrid ItemsSource="{Binding student}">
        <DataGrid.ItemTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Marks}"></DataGrid>
            </DataTemplate>
        </DataGrid.ItemTemplate>
    </DataGrid>
like image 2
Dhaval Patel Avatar answered Oct 06 '22 18:10

Dhaval Patel