ErocM
ErocM

Reputation: 4662

datagrid binding to linq from entity object

This is the first time that I have worked with wpf and I'm lost as to what I am doing wrong. I want to bind my datagrid to my data and any changes made, have it update the database on a button click save.

Here is my C# code:

using System;
using System.Linq;
using System.Windows;
using WebPortalSourceId.data;

namespace WebPortalSourceId
{
  public partial class MainWindow : Window
  {
    private SuburbanPortalEntities entity;
    public MainWindow()
    {
      InitializeComponent();
      entity = new SuburbanPortalEntities();
    }

    private void Search_Click(object sender, RoutedEventArgs e)
    {
      if (CompanyCode.Text.Length != 3)
      {
        MessageBox.Show("Invalid Company Code. It must be 3 characters in length.");
        return;
      }
      FillDataGrid(GetCorporationId(CompanyCode.Text.ToUpper()));
    }

    public void FillDataGrid(Guid corporationId)
    {
      var list = (from s in entity.Sources
        where s.CorporationId == corporationId
        select new SourceRecord
        {
          SourceId = s.SourceId,
          CorporationId = s.CorporationId,
          Description = s.Description,
          IsActive = s.IsActive,
          Name = s.Name,
          TokenId = s.TokenId
        }).ToList();
      SourceDataGrid.ItemsSource = list;
    }
    private Guid GetCorporationId(string companycode)
    {
        return (from cs in entity.CorporationStructures
          where cs.Company == companycode &
                cs.IsActive & 
                cs.Branch == null
          select cs.CorporationId).FirstOrDefault();
    }

    private void Save_Click(object sender, RoutedEventArgs e)
    {
      entity.SaveChanges();
    }

  }

  public class SourceRecord
  {
    public Guid SourceId { get; set; }
    public Guid CorporationId { set; get; }
    public Guid TokenId { set; get; }
    public string Description { set; get; }
    public string Name { set; get; }
    public bool IsActive { set; get; }
  }

}

And my xaml:

<Window x:Class="WebPortalSourceId.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Web Portal SourceId" Height="409" Width="744" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip">
  <Grid>
    <TextBox Name="CompanyCode" HorizontalAlignment="Left" Height="23" Margin="337,11,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="48" MaxLength="3" TextAlignment="Center" CharacterCasing="Lower"/>
    <Label Content="Company Code" HorizontalAlignment="Left" Margin="240,10,0,0" VerticalAlignment="Top"/>
    <DataGrid Name="SourceDataGrid" Margin="10,43,10,10" CanUserReorderColumns="False" CanUserResizeRows="False" HorizontalContentAlignment="Stretch" FontFamily="Microsoft YaHei"/>
    <Button Name="Search" Content="Search" HorizontalAlignment="Left" Margin="390,11,0,0" VerticalAlignment="Top" Width="75" Height="23" Click="Search_Click"/>
    <Button x:Name="Save" Content="Save" HorizontalAlignment="Right" Margin="0,11,183,0" VerticalAlignment="Top" Width="75" Height="23" Click="Save_Click"/>


  </Grid>
</Window>

Not much to it. It does get the data from the database but when I make a change, the changes are not saving back.

What am I doing wrong?

Upvotes: 0

Views: 351

Answers (1)

Okura
Okura

Reputation: 123

You're binding the datagrid with the "list" variable, so any changes you make is affecting only that var, to automatically save the changes you must bind to the entity.Sources collection of your entity model. SourceDataGrid.ItemsSource = entity.Sources

Upvotes: 1

Related Questions