This is a migrated thread and some comments may be shown as answers.

Refreshing a form when editing/inserting with a UserControl

1 Answer 51 Views
Grid
This is a migrated thread and some comments may be shown as answers.
immitev
Top achievements
Rank 1
immitev asked on 09 Dec 2008, 01:52 PM
I have a grid that uses a custom UserControl for editing/inserting

I have two issues, that I am trying to solve for awhile, without success:

1) UserControl does not hide when canceling inserts (only when canceling updates)

        private void bCancel_Click(object sender, ImageClickEventArgs e) 
        { 
            if (_ParentForm != null
            { 
                _ParentForm.ShowReleasesList(_ProductNoderef); 
            } 
 
            GridEditableItem editedItem = Parent.NamingContainer as GridEditableItem; 
            if (editedItem != null
            { 
                editedItem.Edit = false
                editedItem.OwnerTableView.ClearEditItems(); 
                editedItem.OwnerTableView.OwnerGrid.PrepareRowsRecursive(editedItem.OwnerTableView); 
                editedItem.OwnerTableView.OwnerGrid.PrepareRowsVisibilityRecursive(editedItem.OwnerTableView); 
            } 
        } 


2) The grid does not refresh when changing/inserting an item
        private void UpdateGrid(GridEditableItem editedItem) 
        { 
            WorkPackage wp = WorkPackage.GetByHandle(ValDataSet, _WorkPackNoderef); 
             
            IEnumerable<WorkPackDetails> previousWorkPacks = (IEnumerable<WorkPackDetails>)editedItem.OwnerTableView.OwnerGrid.DataSource; 
 
            WorkPackDetails details = editedItem.DataItem as WorkPackDetails; 
            if (details != null
            { 
                // in Update Mode update the workpack details 
                details.FillAttributes(wp); 
                details.FillReleasesDetails(wp, ValDataSet); 
 
                List<WorkPackDetails> updatedWorkpacks = new List<WorkPackDetails>(previousWorkPacks); 
                editedItem.OwnerTableView.OwnerGrid.DataSource = updatedWorkpacks; 
            } 
            else 
            { 
                // In Insert Mode add a new workpack 
                details = WorkPackDetails.GetDetailsFromWorkPack(wp, ValDataSet); 
                List<WorkPackDetails> updatedWorkpacks = new List<WorkPackDetails>(previousWorkPacks); 
                updatedWorkpacks.Add(details); 
                editedItem.OwnerTableView.OwnerGrid.DataSource = updatedWorkpacks; 
            } 
 
            //editedItem.Edit = false; 
            editedItem.OwnerTableView.ClearEditItems(); 
            editedItem.OwnerTableView.OwnerGrid.Rebind(); 
        } 

1 Answer, 1 is accepted

Sort by
0
immitev
Top achievements
Rank 1
answered on 10 Dec 2008, 09:56 AM
Some parts of the code were just wrong. Here is what works:

1) Canceling Edits:

        private void bCancel_Click(object sender, ImageClickEventArgs e) 
        { 
            GridEditableItem editedItem = Parent.NamingContainer as GridEditableItem; 
            if (editedItem != null
            { 
                editedItem.OwnerTableView.IsItemInserted = false;  
                editedItem.OwnerTableView.ClearEditItems(); 
                editedItem.OwnerTableView.OwnerGrid.Rebind(); 
            } 
        } 

2) Updating:

The check for editedItem.DataItem  was just wrong... a good way to do it was checking editedItem.DataSetIndex >= 0

One major nuisance was that the DataSetIndex property seemed to return wrong index when the items were sorted. So I used a workaround with parsing  editedItem.KeyValues which were something like "Noderef=\"325325\""

        private void UpdateGrid(GridEditableItem editedItem) 
        { 
            WorkPackage workPackage = WorkPackage.GetByHandle(ValDataSet, _WorkPackNoderef); 
             
            IEnumerable<WorkPackDetails> previousWorkPacks = (IEnumerable<WorkPackDetails>)editedItem.OwnerTableView.OwnerGrid.DataSource; 
 
            if (editedItem.DataSetIndex >= 0 ) 
            { 
                // in Update Mode update the workpack details 
                string keys = editedItem.KeyValues; 
                int startQuote = keys.IndexOf('"');
                int endQuote = keys.IndexOf('"', startQuote + 1); 
                string sNoderef = keys.Substring(startQuote + 1, endQuote - startQuote - 1); 
                ulong noderef = ulong.Parse(sNoderef); 
 
                List<WorkPackDetails> updatedWorkpacks = new List<WorkPackDetails>(previousWorkPacks); 
                WorkPackDetails editedWorkPack = null
                foreach (WorkPackDetails wpd in updatedWorkpacks) 
                { 
                    if (wpd.Noderef == noderef) 
                    { 
                        editedWorkPack = wpd; 
                        break
                    } 
                } 
 
                // WorkPackDetails editedWorkPack = updatedWorkpacks[editedItem.DataSetIndex]; // does not work 
                editedWorkPack.FillDetailsFromWorkPack(workPackage, ValDataSet); 
                editedItem.OwnerTableView.OwnerGrid.DataSource = updatedWorkpacks; 
            } 
            else 
            { 
                // In Insert Mode add a new workpack 
                WorkPackDetails newWorkPack = WorkPackDetails.GetDetailsFromWorkPack(workPackage, ValDataSet); 
                List<WorkPackDetails> updatedWorkpacks = new List<WorkPackDetails>(previousWorkPacks); 
                updatedWorkpacks.Add(newWorkPack); 
                editedItem.OwnerTableView.IsItemInserted = false;  
                editedItem.OwnerTableView.OwnerGrid.DataSource = updatedWorkpacks; 
            } 
 
            editedItem.OwnerTableView.ClearEditItems(); 
            editedItem.OwnerTableView.OwnerGrid.Rebind(); 
        } 

Tags
Grid
Asked by
immitev
Top achievements
Rank 1
Answers by
immitev
Top achievements
Rank 1
Share this question
or