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
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
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
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
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]}"/>
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With