开发者

Order objects for Northwind Access database

开发者 https://www.devze.com 2022-12-16 04:57 出处:网络
I need to build two objects: an OrderList and an Order. Using those two objects, I have to populate a DataGridView with a history of the orders. However, I am instructed not to use binding sources fo

I need to build two objects: an OrderList and an Order.

Using those two objects, I have to populate a DataGridView with a history of the orders. 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.

Can anyone point me in the right direction? I don't h开发者_JAVA技巧ave much experience with C#.

Thanks.


You could to create an Order class:

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


This is actually a fairly trivial problem. You will need to use these things:

  • DataAdapter (SQL, OleDB, etc)
  • DataGridView (or DataGrid)

Once you have a dataadapter and its associated dataset, you can simply loop to fill the grid.


You can bind to any collection of data that implements IEnumerable because when you call the DataBind() function on the grid it will iterate through the data and bind the grid to the property of each object stored in the collection by reflecting(using reflection) the object and looking for a property that matches the DataField specified for each bound field in the grid.

0

精彩评论

暂无评论...
验证码 换一张
取 消