Print Page | Close Window

Edit a related object in another form

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=759
Printed Date: 19-Sep-2025 at 3:30am


Topic: Edit a related object in another form
Posted By: erturkcevik
Subject: Edit a related object in another form
Date Posted: 09-Apr-2008 at 8:24am
Hi;
 
TblKasa (ID, Code, Name) 
   I have created a Browse Form with the DataGridView to present the records.
   I don't want the user to edit or insert records using the DataGridView (so editing is disabled in this form).
   I have provided three buttons to this form (Insert Record, Change Record and Delete Record).

  When I click Add or Edit, a second form is open to create a new Kasa or edit the selected one.
  Of course, if the user cancel the frmKasa form, the record is not added or edited.
  I want
       - How to passing/accessing selected record from browse form one form in another form
       - After that he saves the changes (PM.SaveChanges) and the current form is closing and the old form with the
         DataGridView gets the focus again. At the point the user is saving the data I want to refresh the data in
         the form with the DataGridView. if the added record I have setting position added record in datagrid.

Please send me example code or application.
 
Thank you for any help me.
 
Platform Windowsforms C# .Net 2  
 
Best Regards

 




Replies:
Posted By: BillG
Date Posted: 13-Apr-2008 at 11:16am

There are probably other ways and better ways to do this but this works well for me and it seems not to violate MVC too bad :)

//CustomersListForm.cs

public class CustomersListForm
{
    private CustomerController myController;

    public CustomersListForm()
    {
       InitializeComponents();
        myController = new CUstomer Controller();
    }

    private void CustomersListForm_Load(object sender, EventArgs e)
    {
        PopulateCustomerGrid();
    }

    public void PopulateCustomerGrid()
    {
          customersGrid.DataSource = myController.GetCustomers();
    }

    private void CustomersGrid_SelChange(object sender, EventArgs e)
    {
         if(CustomersGrid.RowSel != -1)
        {
            Int32 customerId = Int32.Parse(CustomersGrid[CustomersGrid.RowSel,1].ToString();
            myController.GetCustomerById(customerId);
        }
    }

    public void EditCustomer()
    {
         CustomerForm form = new CustomerForm(myController);
         form.ShowDialog();
         PopulateCustomerGrid();
   
    }

    public void AddCustomer()
    {
         myController.CreateCustomer();
         CustomerForm form = new CustomerForm(myController);
         form.ShowDialog();
         PopulateCustomerGrid();        
    }

}


// CustomerController.cs

public class CustomerController
{
      private Customer currentCustomer;
      private PersistenceManager myPM = PersistenceManager.DefaultManager;

      public Customer CurrentCustomer
      {
           get{
                 return currentCustomer;
           }
           set{
                currentCustomer = value;
           }
       }

       public void CreateCustomer()
       {
           currentMember = Customer.Create(myPM);
       }

       public void GetCustomerById(Int32 customerId)
       {
            RdbQuery query = new RdbQuery(typeof(Customer));
            query.AddClause(Customer.CustomerIdEntityColumn, EntityQueryOp.EQ, customerId);
            currentCustomer = myPM.GetEntity<Customer>(query);
       }

       public void SaveCustomer()
       {
             try{
               myPM.SaveChanges();
             }
             catch(SaveException ex)
      {
      }
       }

}


// CustomerForm.cs

public class CustomerForm
{
 private CustomerController myController;

        public CustomerForm(CustomerController controller)
        {
              InitializeComponents();
              myController = controller;
        }

    private void CustomerForm_Load(object sender, EventArgs e)
     {
       CustomerBS.DataSource = myController.CurrentCustomer;
     }

        private void SaveButton_Click(object sender, EventArgs e)
        {
               myController.SaveCustomer();
               this.Close();
         }

       
}
     



Posted By: erturkcevik
Date Posted: 13-Apr-2008 at 11:33pm
Thank yo for reply.
 
First missing declared GetCustomer();
I'm develop mdi application.
Edit for using Show() mode.
User may open edit form count 1 to record count of TblKasa.
Because of separate PM on each editing form for user cancel edit (PM.Rejectchanges).
 
Best Regards
 



Print Page | Close Window