2017-05-27 71 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