Refreshing a form when editing/inserting with a UserControl

2 posts, 0 answers
  1. immitev
    immitev avatar
    9 posts
    Member since:
    Nov 2006

    Posted 09 Dec 2008 Link to this post

    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(); 
            } 
  2. immitev
    immitev avatar
    9 posts
    Member since:
    Nov 2006

    Posted 10 Dec 2008 Link to this post

    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(); 
            } 

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top