datagrid从实体对象绑定到linq

时间:2014-02-19 22:43:11

标签: c# wpf data-binding datagrid

这是我第一次与wpf合作,而我却错失了我的错误。我想将我的数据网格绑定到我的数据和所做的任何更改,让它在按钮单击保存时更新数据库。

这是我的C#代码:

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

}

我的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>

不多。它确实从数据库中获取数据,但是当我进行更改时,更改不会保存回来。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在使用“list”变量绑定datagrid,因此您所做的任何更改都只影响该var,以自动保存您必须绑定到实体模型的entity.Sources集合的更改。     SourceDataGrid.ItemsSource = entity.Sources

相关问题