Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

c# - updating the datagridview in form1 with changes in form2

I have done for clicking one the datagridview row cell in one form say form 1 another form say form 2 will open along with selected data grid view data on form1..

I am using winforms...c#

i have done some operations on datagrid view data and at the end of the operations stage the form2 will be closed

    NOTE :upto this i have  finished

i want to update the datagridview in form1 with changes i have done in form2

for that i have done like this..

form 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);
                   }
               }
      }

and in form2 : I have done like this ...

         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 

i have deleted one row in datagridview by using below code ...its fine..

      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                   
       }

    }

now i want to update the datagridview in form1 how i have to do that .....

can any one have idea about this ...

many thanks....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) write a databind method that accepts a product_id parameter to get data in form 1
2) Before form2.close, intialize form 1 class, and call the method by passing the product_id you just updated and open form1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...