0

Most posts regarding DataGrid AutoGenerateColumns seem to deal with how to circumvent default behavior. Unfortunately the default is what I am struggling with.

I have several database tables created with Entity Framework 6.0, and would like to display them in my View using one and the same Datagrid for all tables, and AutoGenerating the Columns. Desired outcome: for the class-specific Viewmodel bound at run-time, display the columns with headers and at least one row.

XAML of the View's user control:

<UserControl.Resources>
    <DataTemplate x:Key="TableDataTemplate">
        <DataGrid
            AutoGenerateColumns="True"
            GridLinesVisibility="All"
            HorizontalScrollBarVisibility="Visible"
            VerticalScrollBarVisibility="Visible">
        </DataGrid>
    </DataTemplate>
</UserControl.Resources>

<StackPanel Height="720" Width="980">
    <!-- shows user which menu item was chosen -->
    <TextBlock Text="{Binding DisplayName}"/>

    <!-- defines the Input data grid for adding to DB table -->
    <Grid Height="80" MaxHeight="200">
        <ItemsControl
            ItemsSource="{Binding CurrentDataTable}" 
            ItemTemplate="{StaticResource TableDataTemplate}">
        </ItemsControl>
    </Grid>

The user control is bound to a class-specific 'Class'TableViewModel, implemented for all Database Tables. One example:

class ClientsAdminTableViewModel : TableViewModel
{
    // the Property the view binds to
    private IList<Client> currentDataTable;

    public ObservableCollection<Client> CurrentDataTable
    {
        get { return CollectionExtensions.ToObservableCollection<Client>(currentDataTable); }
        set { currentDataTable = value; OnPropertyChanged("CurrentDataTable")}
    }

    public ClientsAdminTableViewModel()
    {
        DisplayName = Strings.ClientAdminDisplayName;
        currentDataTable = context.Clients.ToList<Client>();
    }

When bound to {Binding CurrentDataTable} this code produces a UI, where I can see a grey body (probably the rows) surrounded by horizontal and vertical scrollbars, on top of which one long line (which must be the header line), but no columns.

Any help is appreciated.

2 Answers2

1

your datagrid needs to have its itemsource set

try adding

ItemsSource="{Binding CurrentDataTable}"

to the datagrid declaration. Also, do you get any binding errors in the output window?

J King
  • 4,108
  • 10
  • 53
  • 103
  • did you try including the itemsource binding? – J King Dec 09 '13 at 21:44
  • The ItemsControl contains the ItemsSource line you suggest and an ItemsTemplate statement should allow me to not include a Binding statement in the template. Removing it from the ItemsControl and placing the Binding statement in the template removes the datagrid from the UI altogether. – MartinKleiner Dec 09 '13 at 21:53
  • I would take the datagrid out of the data template and get it working, then try and move it back in. If there are no columns showing up it is not getting the records, this is either a binding issue or a data retrieval issue. – J King Dec 09 '13 at 21:55
  • Thank you J.King! removing the data template and creating the datagrid directly in the grid worked at the outset, with absolutely no change to the code. Since the objective is achieved this is what I will go with - I still wonder where the previous code went wrong. – MartinKleiner Dec 12 '13 at 06:49
0

This code now works (Thank you J. King!) The trick was to get rid of the data template, create the data grid right in the grid with the exact same binding statements.

    <StackPanel Height="720" Width="980">
        <!-- shows user which menu item was chosen -->
        <TextBlock Text="{Binding DisplayName}"/>

        <!-- defines the Input data grid for adding to DB table -->
        <Grid Height="200" MaxHeight="400">

            <DataGrid 
                ItemsSource="{Binding CurrentDataTableNew}" 
                AutoGenerateColumns="True"
                GridLinesVisibility="All" 
                HorizontalScrollBarVisibility="Visible" 
                VerticalScrollBarVisibility="Visible" >
            </DataGrid>
        </Grid>
    </userControl>