[英]Order objects for Northwind Access database
I need to build two objects: an OrderList and an Order. 我需要构建两个对象:OrderList和Order。
Using those two objects, I have to populate a DataGridView with a history of the orders. 使用这两个对象,我必须使用订单历史记录填充DataGridView。 However, I am instructed not to use binding sources for the connection or other drag and drop controls. 但是,指示我不要将绑定源用于连接或其他拖放控件。 Unfortunately, from Google it seems like those are the most popular options for this type of problem. 不幸的是,从Google看来,这些是这类问题的最受欢迎选项。
Can anyone point me in the right direction? 谁能指出我正确的方向? I don't have much experience with C#. 我对C#没有太多经验。
Thanks. 谢谢。
You could to create an Order
class: 您可以创建一个Order
类:
public class Order
{
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
}
Read your database records and load your orders collection 阅读数据库记录并加载订单集合
List<Order> orders = new List<Order>();
using(SqlConnection cn = new SqlConnection("..."))
using (SqlCommand cm = cn.CreateCommand())
{
cn.Open();
cm.CommandText = "SELECT OrderId, OrderDate FROM Orders";
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
orders.Add(new Order()
{
OrderId = dr.GetInt32(dr.GetOrdinal("OrderId")),
OrderDate = dr.GetDateTime(dr.GetOrdinal("OrderDate"))
});
}
}
About GridView part, you should take a look into ASP.NET data binding overview 关于GridView部分,您应该看一下ASP.NET数据绑定概述
This is actually a fairly trivial problem. 这实际上是一个相当琐碎的问题。 You will need to use these things: 您将需要使用以下内容:
Once you have a dataadapter and its associated dataset, you can simply loop to fill the grid. 一旦有了数据适配器及其关联的数据集,就可以简单地循环以填充网格。
您可以绑定到任何实现IEnumerable的数据集合,因为当您在网格上调用DataBind()函数时,它将遍历数据并将网格通过反射(使用反射)绑定到集合中存储的每个对象的属性。对象并寻找与为网格中每个绑定字段指定的DataField匹配的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.