[英]How to populate a datagrid from database?
I have an sql database with data, which I'd like to be shown in the DataGrid. 我有一个包含数据的sql数据库,希望将其显示在DataGrid中。 I've read how to work with a DataGrid, but haven't found information about my case.
我已经阅读了如何使用DataGrid,但是没有找到有关我的案例的信息。 There's a class with properties
有一个带有属性的类
public class OrderModel : INotifyPropertyChanged
{
public int ID { get; set; }
public string Street { get; set; }
public string Building { get; set; }
public string Flat { get; set; }
public string Date { get; set; }
public string Month { get; set; }
public string Year { get; set; }
}
The entity of the database 数据库的实体
public OrdersEntitiesNew db = new OrdersEntitiesNew();
This is how I populate the DataGrid with only Year and I'd like to populate it with everything, how to do that? 这就是我仅用Year填充DataGrid的方法,并且我想用所有数据填充它,该怎么做?
private void Info_OnLoaded(object sender, RoutedEventArgs e)
{
var items = new List<OrderModel>();
Info.ItemsSource= (from s in db.OrderInfoes
group s by s.Year
into result
select new { "Years" = result.Key }).ToList();
}
I know I also can create a list of OrderModel and make it an itemsSource but in this case, how do I populate this list from the database? 我知道我也可以创建OrderModel列表并将其设为itemsSource,但是在这种情况下,如何从数据库中填充此列表?
There's xaml 有xaml
<DataGrid Name="Info" Loaded="Info_OnLoaded" SelectionChanged="Info_SelectionChanged"
Width="568" Canvas.Left="39" Canvas.Top="173" Height="122">
</DataGrid>
Try something like this: 尝试这样的事情:
var model = db.OrderModel;
Info.ItemSource = model.ToList();
You might wish to read more into Linq to get all the information you want to display, hope this helps. 您可能希望阅读更多有关Linq的信息,以获取所有想要显示的信息,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.