2017-05-27 72 views
0

訂單#0001將具有

Color: 1,4,6,8 
PCs: 6,4,3 
Cutting, 30-25,30 

訂單#0002

將比順序#不同0001

Color: 2,8,7,9 
PCs: 3,1,2 
Cutting, 25-30,40 
  • 當我瀏覽訂單號碼時,它會顯示我不同的客戶名稱和訂單詳細信息。

  • 我將輸入訂單明細並點擊「添加到訂單」它將進入下面的datagridview,一旦所有訂單完成,我將按「提交訂單」這是一個完整的記錄。

    • 當我按下「新訂單」時,訂單在datagridview下面,而不是在DataGridView中沒有任何數據的情況下創建一個全新的訂單。

enter image description here

這是我下面的代碼:

void AddRecords() 
    { 
       Connection con = new OrderManager.Connection(); 
       SqlCommand cmd = new SqlCommand(); 
       string query = @"INSERT INTO [MasterDatabase].[dbo].[Neworder] 
     ([OrderID] 
     ,[Date] 
     ,[Customer_Name] 
     ,[Quality] 
     ,[Color] 
     ,[PCs] 
     ,[Cutting] 
     ,[TotalYards]) 
VALUES 
     (@OrderID 
     ,@Date 
     ,@Customer_Name 
     ,@Quality 
     ,@Color 
     ,@PCs 
     ,@Cutting 
     ,@TotalYards)"; 

      cmd = new SqlCommand(query, con.ActiveCon()); 
      pcs = Convert.ToInt32(updown_PCs.Text); 
      cutting = Convert.ToInt32(combo_Cutting.Text); 
      total_yards = pcs * cutting; 
      cmd.Parameters.AddWithValue("@OrderID", TxtBox_OrderID.Text); 
      cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToString()); 
      cmd.Parameters.AddWithValue("@Customer_Name", txtbox_CusName.Text); 
      cmd.Parameters.AddWithValue("@Quality", combo_Quality.Text); 
      cmd.Parameters.AddWithValue("@Color", combo_Color.Text); 
      cmd.Parameters.AddWithValue("@PCs", updown_PCs.Text); 
      cmd.Parameters.AddWithValue("@Cutting", combo_Cutting.Text); 
      cmd.Parameters.AddWithValue("@TotalYards", total_yards); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Record Inserted", "Record Entered Successfully!", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 

     else { MessageBox.Show("Please Check all fields", "Check your Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } 
    } 
+0

首先,您需要在某些屬性中保留CurrentOrder。 – Anton

+0

@安頓試圖想出一些東西但失敗。 – Patrick

+0

要更改訂單,請使用「更新」而不是「插入」。 – jdweng

回答

0

我不知道爲什麼你使用的SqlCommand(可能更好改寫了現代化的ORM(EF或NHibernate的這個應用程序)...但在你的情況下修復錯誤的行爲需要做下一步:

// need create domain type for keep order information 
    // if need you can implement INotifyPropertyChanged 
    public class Order { 
      int  OrderID {get;set} 
      DateTime Date {get;set} 
      string Customer_Name {get;set} 
      int  Quality {get;set} 
      string Color {get;set} 
      string PCs  {get;set} 
      string Cutting {get;set} 
      decimal TotalYards {get;set} 
    } 

    // add property inside your form or viewmodel which is source for your form 
    private Order _currentOrder; 
    public Order CurrentOrder 
    { 
     get{ return _currentOrder ;} 
     set { 
      if (_currentOrder!=value) 
      {   
       FillOrderDetailsBy(_currentOrder.Id) 
      } 
     } 
    } 

    // implement FillOrderDetails (
    void FillOrderDetails(int orderId) 
    { 
     var cmd = new SqlCommand(@"Select * from details", conn) 
     var reader = cmd.ExecuteReader() 

     while (reader.Read()) 
     { 
      // add records to your dataTable which is sour 
     } 
    } 

    // change your newOrder button click handler 
    void bntNewOrder_Click(sender, ars) 
    { 
     CurrentOrder = new Order(); // it will clear your dataGrid 
    } 

    // in case if in your domain you implmenet INotifyPropertyChanged 
    // you can bind you controls to property of this class 
    // this give you fill the power of WinForms dataBinding :)) 
    // for example in your constructor (after line with InitializeComponents()) 
    public YourFormName() 
    { 
    InitializeComponents(); 

    TxtBox_OrderID.DataBindings.Add("Text", CurrentOrder, "OrderID"); 
    dateTimePicker1.DataBindings.Add("Value", CurrentOrder, "Date"); 
    // etc with all controls which are related to Order 
    } 

希望這種方法能夠解決您的問題。

+0

我把這個放在課堂上? – Patrick

+0

Just Order class ...其他代碼應該添加到您的表單中。其實你需要清潔dataGrid時按下AddNewOrder ...所以你可能需要檢查你的處理程序,並在那裏添加清晰dataGrid的代碼。但更恰當的將使用所描述的方法 – Anton

+0

你的意思是這樣的,但這只是清除數據不存儲多個產品在1個訂單號碼'private void Btn_New_Click(object sender,EventArgs e) { CreateNew(); dataGridView1.Rows.Clear(); txtbox_CusName.Clear(); combo_Quality.SelectedIndex = -1; combo_Color.SelectedIndex = -1; updown_PCs.ResetText(); combo_Cutting.SelectedIndex = -1; txtbox_CusName.Focus(); }' – Patrick