我有两个窗体AddProduct和updateproduct
AddProduct窗体中有两个datagridview1和datagridview2 和一个添加按钮
我想在AddProduct窗体中单击一次按钮就弹出updateproduct窗体这个updateproduct获得AddProduct中datagridview1的值和一些其他的值
我用了一个类( ProductInfo)来传到updateproduct中
当updateproduct关闭时AddProduct的datagridview2 添加一条数据
但我的AddProduct窗体添加数据时只能添加一次,datagridview2 中只能显示一次在不知道为什么!public List<ProductInfo> myproducts = new List<ProductInfo>();
AddProduct添加事件中代码
{
 ProductInfo myproduct = new ProductInfo();
updateproduct myupdate = new updateproduct(myproduct);
 myupdate.ShowDialog();
myproducts.Add(myproduct);
 this.dataGridView3.DataSource = mycollection.myproducts;
} ProductInfo myproduct;
        public updateproduct(ProductInfo product)
        {
            InitializeComponent();
            myproduct = product;
            
              }
 private void button1_Click(object sender, EventArgs e)--updateproduct窗体确定事件
        {
            if (txtgoodsprice.Text == "")
                MessageBox.Show("进货价格不能为空!");
            if (txtgoodsunit.Text == "")
                MessageBox.Show("进货单位不能为空!");
            if (txtgoodsnum.Text == "")
                MessageBox.Show("进货数量不能为空!");
            
            myproduct.Goodsbrand = txtgoodsbrand.Text;
            myproduct.Goodscol = txtgoodscol.Text;
            myproduct.Goodsid = txtgoodsid.Text;
            myproduct.Goodsize = txtgoodsize.Text;
            myproduct.Goodsname = txtproname.Text;
            myproduct.Goodsnum = int.Parse( txtgoodsnum.Text);
            myproduct.Goodsprice =int.Parse( txtgoodsprice.Text);
            myproduct.Goodstype = txtgoodstype.Text;
            myproduct.Goodsunit = txtgoodsunit.Text;
            this.Close();        }

解决方案 »

  1.   

    this.dataGridView3是不是this.dataGridView2
    ?
      

  2.   

    对不起写错了
    这里是this.dataGridView2.DataSource = mycollection.myproducts;
      

  3.   

    但我的AddProduct窗体添加数据时只能添加一次,datagridview2 中只能显示一次在不知道为什么!
    -----------------------------------
    这是什么意思?
      

  4.   

    就是说当你单击添加按钮时弹出updateproduct
    窗体设置完值后关闭该窗体将值添加到datagridview2中
    如果在单击添加按钮就无法将从updateproduct窗体中的值添加到datagridview2中
    只能添加一回
    我想要在AddProduct窗体中单击添加按钮一次就在datagridview2中加一条数据
      

  5.   

    datagridview2 再执行一次绑定呢?
      

  6.   

    我这是winform
    好像不能绑定吧
      

  7.   

    AddProduct添加事件中代码
    {
     ProductInfo myproduct = new ProductInfo();
    updateproduct myupdate = new updateproduct(myproduct);
     myupdate.ShowDialog();
    myproducts.Add(myproduct);
     this.dataGridView3.DataSource = mycollection.myproducts;
    }
    -------------------------------
    这是完整的添加按钮单击事件处理代码?
      

  8.   

    myproducts.Add(myproduct);
     this.dataGridView3.DataSource = mycollection.myproducts;
    --------------------------------------------
    上面的两个myproducts是不是同一个?
    这个方法是在mycollection这个类中定义的?
      

  9.   

    public List<ProductInfo> myproducts = new List<ProductInfo>();
    ProductInfo myproduct = new ProductInfo();
      

  10.   

    你的代码基本是对了,但没有理解DATAGRIDVIEW与LIST<>这间的关系,其实你每次增加数据到LIST<>去,数据都有加进去,就是不显示到DATAGRIDVIEW里显示出来
    只要把this.dataGridView3.DataSource = mycollection.myproducts;
    改成this.dataGridView3.DataSource = mycollection.myproducts.ToArray();
    问题就解决了。
      

  11.   

    只要把this.dataGridView2.DataSource = mycollection.myproducts;
    改成this.dataGridView2.DataSource = mycollection.myproducts.ToArray();
    问题就解决了。