我有一个问题,如何使用数据库中的数据填充ListBox而不使用db和大括号列 这是我的代码:
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
a.PlayerOne,
a.PlayerTwo,
a.Date,
a.ResultOne,
a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
答案 0 :(得分:0)
如果你试图返回一个新对象,你必须告诉它属性为
select new {PlayerOne = a.PlayerOne
//More Properties Here
}
因此基于您的示例可能看起来像
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
PlayerOne = a.PlayerOne,
PlayerTwo = a.PlayerTwo,
Date = a.Date,
ResultOne = a.ResultOne,
ResultTwo = a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
这将返回一个没有类型的新对象列表,但属性将在那里
修改强> 如果您希望表中的所有数据 PlayerOne和PlayerTwo等于文本框值,只需执行
var history = db.ResultsHistories.Where(x => x.PlayerTwo == comboBox.Text || x.PlayerOne == comboBox.Text).Select(x => x).ToList();
答案 1 :(得分:0)
尝试添加此内容
“,” +在语句之间
a.PlayerOne +”,“ + a.PlayerTwo +”,“ + a.Date +”,“ + a.ResultOne +”,“ + a.ResultTwo
它将起作用
答案 2 :(得分:-2)
请在这里查看答案: Multicolumn ListBox in WPF
您也可以使用DataGrid来实现此目的。 XAML的代码是:
<DataGrid x:Name="NamesDataGrid"></DataGrid>
和一个非常简单的代码:
public partial class MainWindow : Window
{
private readonly List<Name> _nameList = new List<Name>
{
new Name("Eric","Clapton"),
new Name("Mark", "Knopfler")
};
public MainWindow()
{
InitializeComponent();
NamesDataGrid.ItemsSource = _nameList;
}
}
internal class Name
{
public Name(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
这将是这样的: