using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sample1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      dataGridView1.AutoGenerateColumns = false;
      dataGridView1.AllowUserToAddRows = false;
      dataGridView1.AllowUserToDeleteRows = false;
      dataGridView1.ReadOnly = true;

      textBox1.Enabled = false;

      DataGridViewCell cell = new DataGridViewTextBoxCell();
      DataGridViewColumn col;

      col = new DataGridViewColumn(cell);
      col.DataPropertyName = Northwind.Employee_Sales_by_Country_Table0.OrderID;
      col.HeaderText = "Order ID";
      dataGridView1.Columns.Add(col);

      col = new DataGridViewColumn(cell);
      col.DataPropertyName = Northwind.Employee_Sales_by_Country_Table0.ShippedDate;
      col.HeaderText = "Shipped";
      dataGridView1.Columns.Add(col);

      col = new DataGridViewColumn(cell);
      col.DataPropertyName = Northwind.Employee_Sales_by_Country_Table0.SaleAmount;
      col.HeaderText = "Amount";
      dataGridView1.Columns.Add(col);

      dataGridView1.Width = 60;

      for (int i = 0; i < dataGridView1.Columns.Count; i++)
        dataGridView1.Width += col.Width;

      this.Width = dataGridView1.Left * 3 + dataGridView1.Width;

      DataSet ds = Northwind.Employee_Sales_by_Country(DateTime.Parse("1/1/1990"), DateTime.Parse("1/1/2100"));

      System.Diagnostics.Debug.Assert((int) ds.ExtendedProperties[ParameterDirection.ReturnValue.ToString()] == 0);

      dataGridView1.DataSource = ds.Tables[0];

      dataGridView1_CellClick(this, new DataGridViewCellEventArgs(0, 0));
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      decimal nMax = 0;
      int nOrderID = (int) dataGridView1.Rows[e.RowIndex].Cells[0].Value;

      foreach (DataRow dr in Northwind.CustOrdersDetail(nOrderID).Tables[0].Rows)
      {
        decimal nPrice = (decimal) dr[Northwind.CustOrdersDetail_Table0.ExtendedPrice];

        if (nPrice > nMax)
          nMax = nPrice;
      }

      textBox1.Text = nMax.ToString("C");
    }
  }
}