1

我有 Form1 和我创建的基于服务的数据库中的 DataGridView 和一个客户信息按钮。此按钮打开 Form2,用于添加另一个客户。

我在 Form2 中编写的每条信息都必须在 Form1 DataGridView 中显示。

我该怎么做呢?请帮我!

这是我的Form1代码:

namespace Icabales.Homer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Database1Entities db1Entity = new Database1Entities();
            var myProducts = from p in db1Entity.Products
                             select p;
            cboProduct.DataSource = myProducts;
            cboProduct.DisplayMember = "ProductName";
            cboProduct.ValueMember = "ProductID";
            var myCostumers = from c in db1Entity.Costumers
                              select c;
            dgCostumer.DataSource = myCostumers;
            dgCostumer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            txtStocks.DataBindings.Add("Text", myProducts, "StocksLeft");
            txtUnitPrice.DataBindings.Add("Text", myProducts, "Unitprice");
            txtCostumer.DataBindings.Add("Text", myCostumers, "CostumerName");
            dgCart.Columns.Add("ProductID", "ID");
            dgCart.Columns.Add("ProductName", "Item");
            dgCart.Columns.Add("Quantity", "Quantity");
            dgCart.SelectionMode = DataGridViewSelectionMode.FullRowSelect;                       
        } 

        private void btnAdd_Click(object sender, EventArgs e)
        {   
            dgCart.Rows.Add(cboProduct.SelectedValue, cboProduct.Text, txtQuantity.Text);
        }

            private void btnSaveOrder_Click(object sender, EventArgs e)
            {
            Database1Entities db1Entity = new Database1Entities();
            Order myOrder;
            foreach (DataGridViewRow r in dgCart.Rows)
        {
            if (r.Cells[0].Value != null) //check for each ProductID that is not Empty
            {
                myOrder = new Order(); // create new order for each item in the Cart that has Product ID
                myOrder.ProductID = Convert.ToInt32(r.Cells[0].Value.ToString()); // Product ID
                myOrder.Quantity = Convert.ToInt32(r.Cells[2].Value.ToString()); // Quantity
                myOrder.CostumerID = Convert.ToInt32(dgCostumer.CurrentRow.Cells[0].Value); myOrder.OrderDate = DateTime.Now; //get current Date and Time
                db1Entity.Orders.AddObject(myOrder);
            }
        }
        db1Entity.SaveChanges();
        MessageBox.Show("Your Order has been saved!", "Order", MessageBoxButtons.OK,
        MessageBoxIcon.Information);
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in this.dgCart.SelectedRows)
        {
            dgCart.Rows.RemoveAt(item.Index);
        }
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        dgCart.Rows.Clear();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        new Form3().Show();
    }
}
}

不能发布图片新成员:(

4

0 回答 0