wpf datarow绑定保存更改但不保存插入或删除

时间:2014-02-20 03:40:27

标签: c# wpf datagrid bindingsource

我是wpf的新手,所以这对我来说是一个学习曲线。我有绑定工作,所以我做的任何更新都没有问题。

问题是添加和删除。他们都没有工作。

这是我的代码:

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

namespace WebPortalSourceId
{
  public partial class MainWindow : Window
  {
    private Guid _corporationId;

    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;
      }
      _corporationId = GetCorporationId(CompanyCode.Text.ToUpper());
      FillDataGrid(_corporationId);
    }

    public void FillDataGrid(Guid corporationId)
    {
      var query = from s in entity.Sources where s.CorporationId == corporationId select s;  
      if (query.Any())
      {
        SourceDataGrid.ItemsSource = query.ToList();
      }    
      SourceDataGrid.Columns[2].Visibility = Visibility.Hidden;
      SourceDataGrid.Columns[0].IsReadOnly = true;
      SourceDataGrid.Columns[1].IsReadOnly = true;

    }

    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();
    }

    private void SourceDataGrid_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e)
    {
      var sources = new Source();
      sources.CorporationId = _corporationId;
      sources.Description = string.Empty;
      sources.IsActive = true;
      sources.Name = string.Empty;
      sources.SourceId = Guid.NewGuid();
      sources.TokenId = Guid.NewGuid();
      e.NewItem = sources;
    }

  }

}

我的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="475" Width="948" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
  <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="Upper"/>
    <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" AddingNewItem="SourceDataGrid_AddingNewItem" SelectionMode="Single" SelectionUnit="CellOrRowHeader"/>
    <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" Margin="470,11,397,0" VerticalAlignment="Top" Height="23" Click="Save_Click" HorizontalContentAlignment="Center"/>


  </Grid>
</Window>

我错过了什么?

1 个答案:

答案 0 :(得分:0)

public void FillDataGrid(Guid corporationId)
{
  var query = from s in entity.Sources where s.CorporationId == corporationId select s;
  query.Load(); 

  SourceDataGrid.ItemsSource = entity.Sources.Local;  
  SourceDataGrid.Columns[2].Visibility = Visibility.Hidden;
  SourceDataGrid.Columns[0].IsReadOnly = true;
  SourceDataGrid.Columns[1].IsReadOnly = true;

}

Queryable<T>.ToList()创建了一个List,实体框架对此一无所知。但是,EF仍会保留对从数据库中实例化的每个SourceProxy项的引用。

通过使用DbSet<T>.Local,我们得到一个ObservableCollection,EF已绑定到插入和删除。但是请注意,它是缓慢的。更不用说,现在必须通过UI线程完成对它的所有访问。

因此,即使是对DbSet<T>的数据库访问也必须通过UI线程完成。这很糟糕。

更好的解决方法是创建自己的ObservableCollection,并自行处理Remove / Add