Glory Raj
Glory Raj

Reputation: 17691

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....

Upvotes: 0

Views: 1676

Answers (3)

Ken
Ken

Reputation: 2808

In your Form2 btnProdDelete_Click event before you close the form Set your DialogResult to OK - this.DialogResult = OK; In your using ProductDescriptionForm pf - after you show the dialog - see if the Result is OK if they hit the X it is cancel, After ShowDialog you will now be able to access the public property productid and you can do what you want in form 1 with that productid, you need not create an instance of form 1 - it just happens like magic.

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

Upvotes: 0

DQELER
DQELER

Reputation: 133

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.

Upvotes: 1

Arun
Arun

Reputation: 2523

In Form1 create a BindingSource in Form1 use this a the Datasource for the Grid in Form1. Pass the same Binding source to Form2. Use this as the Datasource for your Grid in Form2.

The changes will be seamless.

Upvotes: 0

Related Questions