I am trying to figure out the reason behind why the datatable acts differently when assigning a list/array to it.
MainDisplay = An empty dockpanel.
LItem = A class with 2 properties, Id and Data
var dgBills = new DataGrid();
dgBills.ItemsSource = new List<ListBillsItem>
{
new LItem {Id = 5, Data = "Patrik"},
new LItem {Id = 6, Data = "Thomas"}
};
MainDisplay.Children.Add(dgBills);
This produces a datagrid with 3 rows!? One row for Patrick, one for Thomas and one (last) empty row.
If i change it into this, then it produces only 2 rows (as it should)
var dgBills = new DataGrid();
dgBills.ItemsSource = new[]
{
new LItem {Id = 5, Data = "Patrik"},
new LItem {Id = 6, Data = "Thomas"}
};
MainDisplay.Children.Add(dgBills);
Anyone that can explain why it behaves like this? I am getting a list back from the database and i would like it to only display the records that are in the list.
This is due to the property
CanUserAddRows="True"on the datagrid that show you an empty row for the new item, if the underlying collection accepts new items. Put this property tofalseand you should see the same in the two case.