【问题标题】:datagrid binding to linq from entity object从实体对象绑定到 linq 的数据网格
【发布时间】:2014-02-19 22:43:11
【问题描述】:

这是我第一次使用 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>

没什么大不了的。它确实从数据库中获取数据,但是当我进行更改时,更改并没有保存回来。

我做错了什么?

【问题讨论】:

    标签: c# wpf data-binding datagrid


    【解决方案1】:

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

    【讨论】:

    • 嗯,如果我这样做了,我如何通过corporateid查询。当我尝试这样做时,我也会得到Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported
    • 你对绑定是对的,我的错,我的意思是我没有解释清楚的是你应该直接绑定到实体集合,避免使用像 SourceRecord 这样的辅助类。我看到它工作的唯一方法是这样的: var query = from s in entity.Sources where s.CorporationId ==corporateId select s if(query.Any()) { SourceDataGrid.ItemsSource = query.ToArray; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多