如何将 WPF Datagrid 绑定到联接表

发布于 2024-08-14 01:44:54 字数 1001 浏览 7 评论 0原文

我有一个大问题。我尝试将 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
         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 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撑一把青伞 2024-08-21 01:44:54

您提供的代码似乎没问题 - 如何创建对象并不重要 - 重要的是对象本身。

您应该显示 xaml,而不是向我们显示此内容。

还有一件事 - 我们是在谈论 winforms 中的 DataGridView 还是 WPF Toolkit 附带的 DataGridView?

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

抱歉。我一开始就错过了 - 你的班级中没有属性!您创建了公共字段而不是属性,这可能就是问题所在。

代码应该如下所示:

 public class NeshtoSi
{
    public NeshtoSi() { }

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

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;}
}
罪#恶を代价 2024-08-21 01:44:54

我最近经历过这个问题,答案在我的一篇文章中概述,位于此处

I went through this recently, and the answer is outlined in a post of mine that is here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文