New Posts New Posts RSS Feed: Edit a related object in another form
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Edit a related object in another form

 Post Reply Post Reply
Author
erturkcevik View Drop Down
Groupie
Groupie


Joined: 14-Jun-2007
Location: Turkey
Posts: 40
Post Options Post Options   Quote erturkcevik Quote  Post ReplyReply Direct Link To This Post Topic: Edit a related object in another form
    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

 



Edited by erturkcevik - 09-Apr-2008 at 8:32am
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post 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();
         }

       
}
     



Edited by BillG - 13-Apr-2008 at 11:19am
Back to Top
erturkcevik View Drop Down
Groupie
Groupie


Joined: 14-Jun-2007
Location: Turkey
Posts: 40
Post Options Post Options   Quote erturkcevik Quote  Post ReplyReply Direct Link To This Post 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
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down