New Posts New Posts RSS Feed: Datagrid and fields on same form solution
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Datagrid and fields on same form solution

 Post Reply Post Reply
Author
DataMan View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Jul-2007
Location: Canada
Posts: 52
Post Options Post Options   Quote DataMan Quote  Post ReplyReply Direct Link To This Post Topic: Datagrid and fields on same form solution
    Posted: 06-Aug-2008 at 9:24am
I thought I would share this just in case anyone else wants to have this functionality.  If anyone else has an easier/better way of solving this issue please let me know.  I wish the datagrids had a BeforeRowChange event with a cancel property.
 
The problem I was trying to solve is having fields on a form but also an infragistics grid used for navigation.  When a user changes the data in the fields to invalid data and then clicked on another record in the grid the binding source would ignore the invalid data and navigate to the clicked on record.
 
I had to add an alreadyinloop flag because the event handler sometimes executed when switching back to the old record even though I turned it off in the code.
 
I also had an isse with the fields going back to the old record but the grid would stay on the new record.  that was fixed with a .Activate.  The only thing I still have to test is if a user deletes a row then will the grid still be in sync with the positions of the binding source.
 
 private void mReportStorageBS_PositionChanged(object sender, EventArgs e)
        {
            mReportStorageBS.PositionChanged -= new EventHandler(mReportStorageBS_PositionChanged);
            NewPosition = mReportStorageBS.Position;
            if (!AlreadyInLoop)
            {
                AlreadyInLoop = true;
                //Go back to the previous position, check for errors if things need to save.
                mReportStorageBS.Position = OldPosition;
                if (SaveRecord(true))
                {
                    //if it saves then go back to the new position
                    mReportStorageBS.Position = NewPosition;
                }
                else
                {
                    ultraGrid1.Rows[OldPosition].Activate();
                }
                AlreadyInLoop = false;
                OldPosition = mReportStorageBS.Position;
            }
           
            mReportStorageBS.PositionChanged += new EventHandler(mReportStorageBS_PositionChanged);
        }
 
My save record routine is a little complicated (not sure if it meets up to the standards of some of the amazing programmers out there,  I'm pretty humble) but I figured I should post it as well.
 
     #region SaveRecord
        /// <summary>
        /// Saves the record.
        /// </summary>
        /// <param name="AskToSave">if set to <c>true</c> [ask to save].</param>
        /// <returns></returns>
        private bool SaveRecord(bool AskToSave)
        {
            mReportStorageBS.EndEdit();
            bool ReturnValue = false;  //This variable is returned to tell the calling process if
            //the save worked or if the user hit cancel or No (same as
            //a successful save)
            ReportStorage currentReportStorage = (ReportStorage)mReportStorageBS.Current;
          
            if (mPersMgr.HasChanges())
            {
               // mPersMgr.SaveChanges();
//Because I have a MDI interface I don't want to save changes to all entities,  just the entity that the user is changing right now.
                EntityList<Entity> saveList = new EntityList<Entity>();
                saveList.ShouldRemoveDeletedEntities = false;
                EntityList<ReportStorage> mChangedReportStorage = new EntityList<ReportStorage>();
                mChangedReportStorage.ReplaceRange(mPersMgr.GetEntities<ReportStorage>(DataRowState.Modified));
                mChangedReportStorage.AddRange(mPersMgr.GetEntities<ReportStorage>(DataRowState.Added));
                mChangedReportStorage.AddRange(mPersMgr.GetEntities<ReportStorage>(DataRowState.Deleted));
                foreach (Entity mChanged in mChangedReportStorage)
                {
                    saveList.Add(mChanged);
                }
                if (saveList.Count != 0)
                {
                    DialogResult result;
                    //something to save
                    // Initializes the variables to pass to the MessageBox.Show method.
                    if (AskToSave)
                    {
                        string message = Properties.Resources.DataSaveMessage;
                        string caption = Properties.Resources.DataSaveCaption;
                        MessageBoxButtons buttons = MessageBoxButtons.YesNoCancel;
                        // Displays the MessageBox.
                        result = MessageBox.Show(message, caption, buttons);
                    }
                    else
                    {
                        result = DialogResult.Yes;
                    }
                    switch (result)
                    {
                        case DialogResult.Cancel:
                            ReturnValue = false;
                            break;
                        case DialogResult.No:
                            //need to undo the changes to the record.
                            mReportStorage[OldPosition].Undo();
                            ReturnValue = true;
                            break;
                        case DialogResult.Yes:
                            try
                            {
                                if (DisplayVerificationResultsForCurrentRecord())
                                {
                                    mPersMgr.SaveChanges(saveList);
                                   
                                    ResetFormAfterSave();
                                    ReturnValue = true;
                                }
                                else
                                {
                                    MessageBox.Show("Errors in data found, changes not saved");
                                   
                                    ReturnValue = false;
                                }
                               
                               
                            }
                            catch (PersistenceManagerSaveException ex)
                            {
                                MessageBox.Show("Errors saving changes to database: \n\n"
                                  + ex.SaveResult.Exception.ToString());
                                this.ultraStatusBar1.Panels[0].Text = Properties.Resources.Status_SaveErrors + " - " + System.DateTime.Now;
                                ReturnValue = false;
                            }
                            break;
                    }
                }
                else
                {
                    this.ultraStatusBar1.Panels[0].Text = Properties.Resources.Status_NothingToSave + " - " + System.DateTime.Now;
                    ReturnValue = true;
                }
            }
            else
            {
                this.ultraStatusBar1.Panels[0].Text = Properties.Resources.Status_NothingToSave + " - " + System.DateTime.Now;
                ReturnValue = true;
            }
            return ReturnValue;
        }
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down