Best way to bind DataGrid to universal list in WPF

I am trying to associate a DataGrid with a shared list in WPF.

The following code results in blank lines for each row of data in my list (i.e. if I have 5 rows, it shows 5 rows but does not show any data in the cells):

 List<DataRow> DataBindingSource = GuestList.Where(row => (row.Field<long>("FK_GROUP_ID") == Int64.Parse(cmbGroup.SelectedValue.ToString())) && (row.Field<long>("FK_AGE_GROUP_ID") != (int)L_Age_Group.Child)) .ToList(); gvwAddultDetails.ItemsSource = DataBindingSource; 

If I convert my list of objects to a DataTable , it works (shows data). For example:

 List<DataRow> DataBindingSource = GuestList.Where(row => (row.Field<long>("FK_GROUP_ID") == Int64.Parse(cmbGroup.SelectedValue.ToString())) && (row.Field<long>("FK_AGE_GROUP_ID") != (int)L_Age_Group.Child)) .ToList(); gvwAdultDetails.ItemsSource = DataBindingSource.CopyToDataTable().DefaultView; 

But if I had a List<DataRow> , how would I convert it to a DataTable ?

What is the best practice for binding a DataGrid to a "list" in WPF?

+6
c # data-binding wpf
source share
1 answer

One way to bind a DataGrid to a universal list in WPF:

MainWindow.xaml.cs

 Grid.DataContext = DataBindingSource; 

MainWindow.xaml

 <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="Grid" /> </Grid> 

But it probably will not work with DataRow , because in WPF, if you want to link something, it must be a property.

- OTHER THOUGHTS -

Here you can convert a generic list to a DataTable :

  • General List in DataTable

How to associate a DataGrid with a shared list in WPF:

  • Why is my WPF Datagrid not showing data?

Especially (great feature in WPF):

  • ObservableCollection<> WPF Example
+5
source share

All Articles