Grid Object disposed error

Apr 30, 2009 at 9:20 AM
Hi,

I get "cannot access a disposed object , Object Name: 'Grid'" error when i close a mdi child form which uses grid object.

My application has a main form (mdi parent) and can open child forms (mdi child). Mdi child forms are same type and they use sourcegrid. I used grid component and added a image button to delete currently delete button clicked row. So each row has this column format

Text 1 - Text 2 - Delete Image Button (has event on click)

Event removes button clicked row. Event has only below code which works.

The error occurs when i click delete image on grid AND then save&close child form. I implemented a button that can add new items to grid. When i add new item AND save&close child form, no error occurs. But when i remove row and close child form it gives error always. Here is the functions i use for grid.

        private void DeleteSelectedRow(object sender, EventArgs e)
        {
            if (grid.Selection.GetSelectionRegion().GetRowsIndex().Length > 0)
                grid.Rows.Remove(grid.Selection.GetSelectionRegion().GetRowsIndex()[0]);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            int row = grid.RowsCount;
            grid.Rows.Insert(row);

            grid[row, 0] = new SourceGrid.Cells.Cell("", mTextEditor);
            grid[row, 1] = new SourceGrid.Cells.Cell("", mLinkEditor);
            grid[row, 2] = GenerateDeleteButton();
        }

        private SourceGrid.Cells.Cell GenerateDeleteButton()
        {
            SourceGrid.Cells.Cell btnDelete = new SourceGrid.Cells.Cell();
            btnDelete.Image = Images.LinkDelete;
            SourceGrid.Cells.Controllers.Button btnController = new SourceGrid.Cells.Controllers.Button();
            btnController.Executed += new EventHandler(DeleteSelectedRow);
            btnDelete.AddController(btnController);
            SourceGrid.Cells.Views.Cell btnDeleteView = new SourceGrid.Cells.Views.Cell();
            btnDeleteView.BackColor = SystemColors.Window;
            btnDeleteView.ForeColor = Color.White;
            btnDeleteView.ImageAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;

            btnDelete.View = btnDeleteView;
            btnDelete.ToolTipText = "Delete Link";

            return btnDelete;
        }


I suspect my delete code is not valid so it causes application to crash. I am not accessing the grid object after i close the child form. Any help is appreciated.

Thanks
Apr 30, 2009 at 12:31 PM
Hi,

After some research in sourcegrid sources, i found out the error.

I delete the row with above pasted method and although row is deleted SourceGrid.Cells.Controllers.MouseSelection.mScrollTimer_Tick still continues to tick. Because EndScrollTracking() is not called so ticker never stops.

I had to modify sourcegrid code. I pasted below here. I

modified SourceGrid.Cells.Controllers.MouseSelection.mScrollTimer_Tick function:

        private void mScrollTimer_Tick(object sender, EventArgs e)
        {
            if (mCapturedGrid == null )
                return;

            if (! mCapturedGrid.IsDisposed)
            {
                //Scroll the view if required
                Point mousePoint = mCapturedGrid.PointToClient(Control.MousePosition);
                mCapturedGrid.ScrollOnPoint(mousePoint);
            }
            // else EndScrollTracking( mCapturedGrid);  <= didnot test this but maybe it works too
        }

I hope it helps for others who get this error. I am open to any other delete row method.

Thanks...
Coordinator
May 13, 2009 at 2:07 PM

Thanks for  telling about your findings!

I've included your suggestion into SourceGrid, so that same bug wount happen again.   That part with timer is a tricky one, and it caused some troubles a few times already.

 

Thanks!