basic design question: classes and datasources

G

Guest

I'm new to designing, here's my question:

Say, for example, I have a class Order and it contains an array of
OrderDetail Objects and it contains an array of Customer objects. I have a
grid on my form and I want this grid to display some data from Order class,
some data from OrderDetail class and some data from Customer class.

I really want to learn about design and what would be a good way to handle
this situation? Should I create another class that will contain just the
variables from the other three classes that the grid will display. And set
the grids datasource to this new class?
 
B

bob

Hi,
There are two issues I see here.

If your data is traditional Customer / order / order details
held in a database then you may want to consider using a dataset
to hold the data and DataGridView / binding source objects to give you
the display of the data's relationships.
With this scenario assuming you are using a Datagridview control(s) in
a Winform app.
The basic idea is one Datagridview per table.
The bindingsource object for the Customers DatagridView is bound to
the Dataset customer table
The bindingsource object for the Orders has a Datarelation to the
customer bindingsource object and so on down the chain.
When you click a customer in its grid the order grid responds.
Likewise if you had a third grid / bindingsource for orderdetails it
would respond also.

If you are committed to pulling the data out of the database straight
into a class structure then are you sure your class description is
what you have?
I would have thought a Customer class contained an array of Orders
each of which contained an array of OrderDetails.
This class structure does not lend itself to a single datagridview
display.
e.g. How do you display the order with say 100 details sensibly?

It does lend itself however to a treeview where Customer nodes would
contain Order nodes which would contain OrderDetail nodes

I suggest that you are best served to stay in the mainstream of
(DataSet / DataAdapter / Bindingsources)
Build a solid DataAccess Layer (Class) that fills / Updates the
dataset(s).

Snippet from a multigrid form follows.
The form displays a grid of suppliers, Batches, Vouchers.
(A Supplier has many batches, each batch has many vouchers)
When a supplier is clicked it filters the batch grid.
The relationshiip Batch to Voucher forces the Voucher grid to
respond.

regards
Bob

public partial class frmVoucherView : Form
{
readonly BindingSource mbsVoucher;
readonly BindingSource mbsBatch;
//BindingSource mbsSupplier;
dsAllVouchers mDs;
readonly DT mDt; //DATA ACCESS LAYER CLASS
public frmVoucherView()
{
InitializeComponent();
mDt = new DT();
mbsVoucher = new BindingSource();
mbsBatch = new BindingSource();


}

private void LoadDataGrids()
{
mDs = new dsAllVouchers();
if (!mDt.GetVoucherView(mDs))// LOAD TYPED DATASET WITH
OUR DATA
return;
mbsBatch.DataSource = mDs;
mbsBatch.DataMember = mDs.Tables["Batch"].TableName;//This
is the base table so no need for a datarelation declaration
mbsVoucher.DataSource = mbsBatch;//Joinng the two binding
sources
mbsVoucher.DataMember =
mDs.Relations["FK_Batch_Voucher"].RelationName;// this is the one to
many relationship between batches and vouchers
dgvSupplier.DataSource = mDs;
dgvSupplier.DataMember = mDs.Tables["Supplier"].TableName;
// The Supplier table is standalone it will filter the Batch table
dgvVoucher.Columns[0].Width = 0;//Hiding columns not
interested in.
dgvVoucher.Columns[6].Width = 0;
dgvBatch.Columns[1].Width = 0;
}
private void frmVoucherView_Load(object sender, EventArgs e)
{
dgvBatch.DataSource = mbsBatch;
dgvVoucher.DataSource = mbsVoucher;
LoadDataGrids();//Get the data into the dataset
}
private void cmdShowAllBatches_Click(object sender, EventArgs
e)
{
mbsBatch.Filter = null;
}
private void cmdShowSupplier_Click(object sender, EventArgs e)
{
mbsBatch.Filter = "supplier_id=" + "'" +
dgvSupplier.CurrentRow.Cells[0].Value + "'";//Filtering the Batch
bindingsource Causes the Voucher bindingsource to respond
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top