views:

79

answers:

3

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 have much experience with C#.

Thanks.

+6  A: 

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

Rubens Farias
Good code sample, thanks for not being a jerk and helping the guy out. +1
Achilles
Thanks Rubens! This was more than helpful!
Jack
Nice to hear that, Jack! Good luck!
Rubens Farias
A: 

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.

GrayWizardx
A: 

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.

Achilles