【问题标题】:updating the datagridview in form1 with changes in form2使用 form2 中的更改更新 form1 中的 datagridview
【发布时间】:2011-08-19 16:04:52
【问题描述】:

我已经完成了单击一个表单中的 datagridview 行单元格的操作,比如说表单 1,另一个表单说表单 2 将与表单 1 上选定的数据网格视图数据一起打开。

我正在使用 winforms...c#

我已经对datagrid视图数据做了一些操作,在操作阶段结束时form2将被关闭

    NOTE :upto this i have  finished

我想用我在 form2 中所做的更改来更新 form1 中的 datagridview

为此我已经这样做了..

表格1:

     private void productGridview_Cellclick(object sender, DataGridViewCellEventArgs e)
     {

         if (e.ColumnIndex != productgridview.Columns["productimage"].Index) return;

            if (productgridview.SelectedCells.Count == 0) return;

            int selectedrowindex= productgridview.SelectedCells[0].RowIndex;

            DataGridViewRow selectedRow = productgridview.Rows[selectedrowindex];
              if (img is Image)
               {
                   using (ProductDescriptionForm pf = new ProductDescriptionForm())
                   {

                       pf.picture = img;
                       pf.productname = productname;
                       pf.description = desc;
                       pf.productprice = productprices;
                       pf.categoryname = categoryCombobox.Text;
                       pf.productid = productids;
                       pf.ShowDialog(this);
                   }
               }
      }

在form2中:我已经这样做了......

         public int productid
    {
        get { return _prodid; }
        set { _prodid = value; }

    }
    public Image picture
    {
        get { return pictureBox1.Image; } 
        set { pictureBox1.Image = value; }
    }
   like this  some constructors  i have used and then 

我已经使用下面的代码删除了 datagridview 中的一行...很好..

      private void btnProdDelete_Click(object sender, EventArgs e)
      {
        using(var context = new TsgEclipseEntities())
        {
              var pd = new product(){ product_Id = productid };
              context.products.Attach(pd);
              context.DeleteObject(pd);
              context.SaveChanges();
              this.Close();   // form2 close                   
       }

    }

现在我想更新 form1 中的 datagridview 我该怎么做.....

任何人都可以对此有所了解...

非常感谢....

【问题讨论】:

  • 你的数据源 MS-SQL 是什么?
  • 不,我的数据库是 mysql,我正在使用 mysql 工作台检索数据,然后我将数据连接到 model.edmx

标签: c# .net winforms data-binding datagridview


【解决方案1】:

1) 编写一个接受product_id参数的databind方法来获取表格1中的数据
2)在form2.close之前,初始化form 1类,传入刚刚更新的product_id调用方法,打开form1。

【讨论】:

    【解决方案2】:

    在 Form1 中创建一个 BindingSource,在 Form1 中使用这个 Datasource 用于 Form1 中的 Grid。将相同的绑定源传递给 Form2。将其用作 Form2 中网格的数据源。

    变化将是无缝的。

    【讨论】:

    • 我已经在 form1 中定义了 productbindingsource,你的意思是我必须在 form2 中使用这个 bindingsource 吗?请问我的语法如何..
    • 我在form 2中没有任何datagridview ....用于在form2中指定bindingsource ...
    • 对不起,我误会你Form2中有另一个Grid。
    • 您可能希望使用 Form1 中的 BindingSource 传递给 Form2。在 Form2 中,使用 BindingSource.Current - 这将为您提供您在 Form1 的网格中选择的当前行。您可以在 Form2 的 Load Event 中使用它来绑定到 Form2 中的各个 UI 元素。
    • 您可能希望在 Form2 中使用的语法是:<yourForm2sTextBox>.DataBindings.Add("Text", <yourBindingSource>, "<ColumnName>");
    【解决方案3】:

    在关闭表单之前在 Form2 btnProdDelete_Click 事件中将 DialogResult 设置为 OK - this.DialogResult = OK; 在您使用 ProductDescriptionForm pf 中-显示对话框后-查看结果是否正常,如果他们点击 X 则取消, 在 ShowDialog 之后,您现在可以访问公共属性 productid 并且您可以使用该 productid 在表单 1 中做您想做的事情,您无需创建表单 1 的实例 - 它就像魔术一样发生。

    A quick easy example of passing data from form2 to form 1

    【讨论】:

    • 请改进你的答案格式,更多的文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多