locked
How to binding DataGrid column to EF table RRS feed

  • Question

  • Hi All.

    I have form with datagrid. How to binding columns of datagrid to EF table. That is code of datagrid:

    <DataGrid Name="F_Employee_DG" ToolTip="View and edit employees">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Width="150" SortMemberPath="FirstName" IsReadOnly="True"/>
            <DataGridTextColumn Header="Last Name" Width="150" SortMemberPath="LastName" IsReadOnly="True"/>
            <DataGridTextColumn Header="Email Address" Width="150" SortMemberPath="EmailAddress" IsReadOnly="True"/>
            <DataGridTemplateColumn Header="Edit">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="btnEdit" Content="Edit"  />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

    Will appreciate for detail explanation. Thanks.

    • Moved by CoolDadTx Tuesday, May 19, 2020 1:46 PM WPF related
    Monday, May 18, 2020 3:25 PM

All replies

  • WPF can be discussed at its forum.

    https://docs.microsoft.com/answers/topics/wpf.html

    Monday, May 18, 2020 4:20 PM
  • You can check out the following starting with the XAML above the section Adding a Details grid. You need to set the Grid's DataContext.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 18, 2020 5:34 PM
  • I change XAML to:

    <DataGrid Name="F_Employees_DG" ItemsSource="{Binding Path={}}" ToolTip="View and edit users" Style="{StaticResource DataGridStyle}"> <DataGrid.Columns> <DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}" Width="150" SortMemberPath="FirstName" IsReadOnly="True"/> <DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}" Width="150" SortMemberPath="LastName" IsReadOnly="True"/> <DataGridTextColumn Header="Email Address" Binding="{Binding Path=EmailAddress}" Width="150" SortMemberPath="EmailAddress" IsReadOnly="True"/> <DataGridTextColumn Header="Department Name" Binding="{Binding Path=DepartmentName}" Width="150" SortMemberPath="DepartmentName" IsReadOnly="True"/>    <DataGridTemplateColumn Header="Ed" Width="32">

            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Name="EditRecord" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" ToolTip="Click to edit record">
                        <Image Source="Resources/EditB.png" Height="20" Width="20" />
                    </Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid>


    And C# the Load method has code:

    using (Model1 context = new Model1())
    {
       Employee_DG.ItemsSource = context.Employees.ToList();
    }

    The problem is fixed. For me need to figure out how to populate and filter DataGrid by TextBox value in Data Layer of the project.

    Thanks.






    • Edited by zleug Wednesday, May 20, 2020 6:44 PM
    Wednesday, May 20, 2020 3:46 AM