示例#1
0
        private async void processData(bool isEdit)
        {
            string custId = customers.ElementAt(selectCustomerComboBox.SelectedIndex).id;

            if (isEdit)
            {
                if (dataGridView1.CurrentCell != null)
                {
                    int index = dataGridView1.CurrentCell.RowIndex;
                    var data  = vouchers.ElementAt(index);
                    data.code       = voucherCode;
                    data.customerId = custId;
                    data.usedCount  = int.Parse(usedCountTextBox.Text);

                    if (!MessagePrompt.displayPrompt("Edit", "edit this voucher"))
                    {
                        return;
                    }
                    bool success = await DatabaseOperations.editVoucher(data);

                    if (success)
                    {
                        MessageBox.Show("Data updated successfully");
                        loadItems();
                        dataGridView1.Rows.Clear();
                        loadItemsIntoDataGrid();
                        selectCustomerComboBox.Text = dgVouchersList.ElementAt(index).name;
                    }
                }
            }
            else
            {
                VoucherDataModel voucher = new VoucherDataModel()
                {
                    code       = voucherCode,
                    customerId = custId,
                    usedCount  = int.Parse(usedCountTextBox.Text)
                };

                if (!MessagePrompt.displayPrompt("Create New", "create new voucher"))
                {
                    return;
                }

                DatabaseOperations.addVoucher(voucher);

                MessageBox.Show("Vouchers created successfully");

                loadItems();
                selectCustomerComboBox.Text = "";

                dataGridView1.Rows.Clear();
                loadItemsIntoDataGrid();
            }
        }
 public void addVoucher(VoucherDataModel voucher, string name, DataGridView datagridView)
 {
     datagridView.Rows.Add((datagridView.RowCount + 1).ToString(), voucher.code, name, voucher.usedCount);
 }