如何将 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提供的代码似乎没问题 - 如何创建对象并不重要 - 重要的是对象本身。
您应该显示 xaml,而不是向我们显示此内容。
还有一件事 - 我们是在谈论 winforms 中的 DataGridView 还是 WPF Toolkit 附带的 DataGridView?
=========================================
抱歉。我一开始就错过了 - 你的班级中没有属性!您创建了公共字段而不是属性,这可能就是问题所在。
代码应该如下所示:
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:
我最近经历过这个问题,答案在我的一篇文章中概述,位于此处
I went through this recently, and the answer is outlined in a post of mine that is here