예제 #1
0
        private void btnNewProduct_Click(object sender, EventArgs e)
        {
            CustomerInfoEditorForm custInfoEditForm = new CustomerInfoEditorForm(ref customerList);
            if (custInfoEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                CustomerInfo newCust = custInfoEditForm.GetCustomerObject();
                newCust.SetNewOrder(customerList.Count + 1);

                // Insert and Update List
                // List<CustomerInfo> res = customerList.Where(ex => ex.Order >= newCust.Order).ToList<CustomerInfo>();
                /*
                // Positive Value
                if (newCust.Order > 0)
                {
                    // Incase Of New
                    if (res.Count == 0)
                        newCust.SetNewOrder(customerList.Count + 1);
                    else
                    {
                        // Incase of Inserted Order
                        for (int i = 0; i < res.Count(); i++)
                            res[i].SetNewOrder(res[i].Order + 1);
                    }
                }
                // Negative Value
                else
                {
                    newCust.SetNewOrder(1);
                    for (int i = 0; i < customerList.Count; i++)
                        customerList[i].SetNewOrder(customerList[i].Order + 1);
                }
                */

                customerList.Add(newCust);
                ListAllCustomers();
                dgvGeneral.FirstDisplayedScrollingRowIndex = dgvGeneral.RowCount - 1;
            }
            custInfoEditForm.Dispose();
        }
예제 #2
0
        private void dgvGeneral_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            string custName = dgvGeneral.Rows[e.RowIndex].Cells[1].Value.ToString();
            CustomerInfo res = customerList.Where(ex => ex.CustomerName == custName).Single();

            if (e.ColumnIndex == 4)
            {
                // EDIT
                CustomerInfoEditorForm custInfoEditForm = new CustomerInfoEditorForm(res);
                if (custInfoEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    int idx = customerList.IndexOf(res);
                    customerList.RemoveAt(idx);

                    CustomerInfo editedCust = custInfoEditForm.GetCustomerObject();

                    /*
                    // If order is 0, then became the first order
                    if (editedCust.Order <= 1)
                    {
                        // SET TO TOP OF THE LIST
                        int oldOrder = res.Order;
                        editedCust.SetNewOrder(1);

                        // Upper zone
                        for (int i = 0; i < oldOrder - 1; i++ )
                            customerList[i].SetNewOrder(customerList[i].Order + 1);

                        customerList.Insert(idx, editedCust);
                    }
                    else if (editedCust.Order >= customerList.Count)
                    {
                        // SET TO BUTTOM OF THE LIST
                        int oldOrder = res.Order;
                        editedCust.SetNewOrder(customerList.Count + 1);

                        // Lower zone
                        for (int i = oldOrder - 1; i < customerList.Count; i++)
                            customerList[i].SetNewOrder(customerList[i].Order - 1);

                        customerList.Insert(idx, editedCust);
                    }
                    else
                    {
                        // SET TO THE MIDDLE OF THE LIST
                        int oldOrder = res.Order;

                        // Detec to move up or move down
                        if (oldOrder > editedCust.Order)
                        {
                            // Move Up
                            for (int i = editedCust.Order - 1; i < oldOrder - 1; i++)
                                customerList[i].SetNewOrder(customerList[i].Order + 1);
                        }
                        else if (oldOrder < editedCust.Order)
                        {
                            // Move Down
                            for (int i = oldOrder - 1; i < editedCust.Order - 1; i++)
                                customerList[i].SetNewOrder(customerList[i].Order - 1);
                        }

                        customerList.Insert(idx, editedCust);
                    }

                    */

                    // Sort to keep order in the right place
                    customerList = customerList.OrderBy(ex => ex.Order).ToList<CustomerInfo>();

                    ListAllCustomers();
                }
                custInfoEditForm.Dispose();
            }
            else if(e.ColumnIndex == 5)
            {
                // DELETE
                string message = "คุณต้องการลบข้อมูลลูกค้า คุณ " + custName + " ออกจากระบบ\r\nใช่ หรือ ไม่";
                if (MessageBox.Show(message, "กรุณายืนยัน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                {
                    if (customerList.Count == 1)
                    {
                        MessageBox.Show("ขออภัย\r\nข้อมูลลูกค้าในสาย ต้องมีขั้นต่ำ 1 คน");
                        return;
                    }

                    customerList.Remove(res);
                    ReorderCustomersList();
                    ListAllCustomers();
                }
            }
            else if(e.ColumnIndex == 6)
            {
                if (e.RowIndex == -1)
                    return;

                CustomerInfo item = customerList[e.RowIndex];
                if (e.RowIndex > 1)
                {
                    customerList.RemoveAt(e.RowIndex);
                    customerList.Insert(e.RowIndex - 1, item);
                    ReorderCustomersList();
                    ListAllCustomers();
                    dgvGeneral.FirstDisplayedScrollingRowIndex = e.RowIndex - 1;
                }
            }
            else if(e.ColumnIndex == 7)
            {
                if (e.RowIndex == -1)
                    return;

                CustomerInfo item = customerList[e.RowIndex];
                if (e.RowIndex < customerList.Count - 1)
                {
                    customerList.RemoveAt(e.RowIndex);
                    customerList.Insert(e.RowIndex + 1, item);
                    ReorderCustomersList();
                    ListAllCustomers();
                    dgvGeneral.FirstDisplayedScrollingRowIndex = e.RowIndex + 1;
                }
            }
        }