Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top