如何将WPF Datagrid绑定到连接的表 [英] How to bind WPF Datagrid to a joined table

查看:148
本文介绍了如何将WPF Datagrid绑定到连接的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的问题。我尝试将我的WPF DataGrid 绑定到使用内部连接创建的表。我已经创建了一个类,以便成功转换信息:

  public class NeshtoSi 
{
public NeshtoSi (){}

public string ssn;
public string name;
public string surname;
}

然后我创建内连接表。当我分配 ItemsSource 并且所有值都正确传输时,但是$ DataGrid 不可视化它们。

  var dd = from d in dataContext.Medical_Examinations 
将p连接到dataContext.Patients中,d.SSN等于p.SSN
选择新的NeshtoSi {ssn = d.SSN,name = p.Name,surname = p.Surname};


IQueryable< NeshtoSi> sQuery = dd;

if(!string.IsNullOrEmpty(serName.Text))
sQuery = sQuery.Where(x => x.name.Contains(serName.Text));
if(!string.IsNullOrEmpty(serSurame.Text))
sQuery = sQuery.Where(x => x.surname.Contains(serSurame.Text));
if(!string.IsNullOrEmpty(serSSN.Text))
sQuery = sQuery.Where(x => x.ssn.Contains(serSSN.Text));

var results = sQuery.ToList();

AnSearch.ItemsSource = sQuery;

我希望有人可以帮助我...

解决方案

您所呈现的代码似乎可以 - 如何创建对象并不重要 - 重要的是对象本身。



而不是告诉我们,你应该显示xaml。



还有一件事 - 我们在谈论来自winform的DataGridView还是WPF Toolkit附带的DataGridView?



==================== =================



对不起,我第一次错过了 - 你的班上没有财产您已经创建了公共字段而不是属性,这可能是问题。



代码应如下所示:

  public class NeshtoSi 
{
public NeshtoSi(){}

public string ssn {get; set;}
public string name {get; set;}
public string surname {get; set;}
}


I have a big problem. I try to bind my WPF DataGrid to a table, created with inner join. I have created a class for the info to convert successfully:

public class NeshtoSi
{
    public NeshtoSi() { }

    public string ssn;
    public string name;
    public string surname;
}

And then I create the inner-joined tables. Still when I assign the ItemsSource and all values are transferred properly, but the DataGrid does not visualize them.

var dd = from d in dataContext.Medical_Examinations
         join p in dataContext.Patients on d.SSN equals p.SSN
         select new NeshtoSi { ssn = d.SSN, name = p.Name, surname = p.Surname };


IQueryable<NeshtoSi> sQuery = dd;

if (!string.IsNullOrEmpty(serName.Text))
    sQuery = sQuery.Where(x => x.name.Contains(serName.Text));
if (!string.IsNullOrEmpty(serSurame.Text))
    sQuery = sQuery.Where(x => x.surname.Contains(serSurame.Text));
if (!string.IsNullOrEmpty(serSSN.Text))
    sQuery = sQuery.Where(x => x.ssn.Contains(serSSN.Text));

var results = sQuery.ToList();

AnSearch.ItemsSource = sQuery;

I hope that someone can help me...

解决方案

The code that you presented seems ok - it doesn't matter how an object is created - what matters is the object itself.

Rather than showing us this, you should show the xaml.

One more thing - are we talking about DataGridView from winforms or rather the one that comes with WPF Toolkit ?

=======================================

Sorry. I've missed it in the first place - you don't have properties in your class! You've created public fields instead of properties and that's probably the problem.

The code should look like this:

 public class NeshtoSi
{
    public NeshtoSi() { }

    public string ssn{get; set;}
    public string name{get; set;}
    public string surname{get; set;}
}

这篇关于如何将WPF Datagrid绑定到连接的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆