SaveChanges on LINQ to EF Datagrid Projection

  • Thread starter Thread starter Justair07
  • Start date Start date
J

Justair07

Guest
Hello,

Forgive me but I'm new to LINQ and EF and I may be way off base.

I have a WPF datagrid that I want my users to be able to save changes to. I have a custom class that projects the correct datatype (not anonymous) and the datagrid compiles great. How do I save my changes. For example, if I enter data in the ActualSavings field, I want it to save back to the database. Here is my attempt.

Here is the Custom Class:

public class ProjectsBySite
{
public int IDSite { get; set; }
public string SiteName { get; set; }
public int IDProjects { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ProjectName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime SavingsDate { get; set; }
public decimal PlannedSavings { get; set; }
public decimal ActualSavings { get; set; }
}

Here is my cs with the RowEditEnding Event (error:'Data' does not exist in the current context on Data.SaveChages):

public MainWindow()
{
InitializeComponent();
dgProj.ItemsSource = GetProjectsBySiteID(1).ToList();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{

}

public List<ProjectsBySite> GetProjectsBySiteID(int siteid/*<---*/)
{
var prjs = new List<ProjectsBySite>();
using (ProductivityEntities context = new ProductivityEntities())
{
prjs = (from s in context.pt_Site
join ps in context.pt_ProjectsSites
on s.IDSite equals ps.Site_id
join p in context.pt_Projects
on ps.Project_id equals p.IDProjects
join pz in context.pt_ProjectSavings
on p.IDProjects equals pz.Project_id
join l in context.pt_Personnel
on p.Personnel_id equals l.IDPersonnel
where s.IDSite == siteid
select new ProjectsBySite
{
IDSite = s.IDSite,
SiteName = s.SiteName,
IDProjects = p.IDProjects,
FirstName = l.FirstName,
LastName = l.LastName,
ProjectName = p.ProjectName,
StartDate = p.StartDate,
EndDate = p.EndDate,
SavingsDate = pz.SavingsDate,
PlannedSavings = pz.PlannedSavings ?? 0,
ActualSavings = pz.ActualSavings ?? 0
}).ToList();
}

return prjs;
}
private void dgProj_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
ProjectsBySite proj = e.Row.DataContext as ProjectsBySite;
Data.SaveChanges();
}
}

And here is my xaml:

<Window x:Class="Productivity_Tracking_Application.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Productivity_Tracking_Application"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="0,0,18,0">
<DataGrid x:Name="dgProj" HorizontalAlignment="Left" Height="379" Margin="10,10,0,0" VerticalAlignment="Top" Width="764"
AutoGenerateColumns="False" RowEditEnding="dgProj_RowEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding IDSite, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Site ID" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding SiteName, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Site Name"/>
<DataGridTextColumn Binding="{Binding IDProject, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Project ID"/>
<DataGridTextColumn Binding="{Binding FirstName, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" Header="First Name"/>
<DataGridTextColumn Binding="{Binding LastName, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Last Name" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding ProjectName, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Project Name"/>
<DataGridTextColumn Binding="{Binding StartDate, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Start Date"/>
<DataGridTextColumn Binding="{Binding EndDate, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" Header="End Date"/>
<DataGridTextColumn Binding="{Binding SavingsDate, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Savings Date" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding PlannedSavings, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Planned Savings"/>
<DataGridTextColumn Binding="{Binding ActualSavings, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Header="Actual Savings"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

Continue reading...
 
Back
Top