How can i delete the selected rows?

Posted by: floibl on 8 September 2017, 2:40 pm EST

  • Posted 8 September 2017, 2:40 pm EST

    Hello all,

    i am struggling trying to delete the Selected Rows from a FPSpread.

    I first get the selected Rowindices like this:

     private IEnumerable GetSelectedRowIndices()
            {
                CellRange[] r = grid_Sheet1.GetSelections();
                List rows = new List();
                foreach (CellRange cellRange in r)
                {
                    if (cellRange.RowCount <= 0)
                    {
                        continue;
                    }

                    if (cellRange.RowCount.Equals(1))
                    {
                        rows.Add(cellRange.Row);
                    }

                    int upperBound = cellRange.Row + cellRange.RowCount;
                    for (int i = cellRange.Row; i < upperBound; i++)
                    {
                        rows.Add(i);
                    }
                }

                rows = rows.Distinct().ToList();
                rows.Sort();
                return rows;
            }


    then i try to

     foreach (int row in rows)

                    {

                        grid_Sheet1.Rows[row].Remove();

                        grid_Sheet1.RemoveRows(row, 1);

                    }

     

    And both methods just don't work. Literally nothing happens? No rows are ever removed, no Exception no nothing... I just don't get it...?

    Could it have something to do with my DataModel? I created a custom ISheetDataModel impl.

     

  • Replied 8 September 2017, 2:40 pm EST

    Hmm... u are using GetSelection instead of GetSelections and then passing that result directly to RemoveRows... not so different from my Code. Mine just tries to get all Rowindices which are affected beforehand, is that wrong?. The Rowindices i get from my method are ok so is the RemoveRows(row, 1) call the problem?
  • Replied 8 September 2017, 2:40 pm EST

    Hello,

    Please have a look at the attached sample where I can get all the selected rows deleted with no issues. Let me know if this doesn't help you.

     

    Thanks,

     


    2011/06/WindowsApplication14.zip
  • Replied 8 September 2017, 2:40 pm EST

    Btw i have the following options set, maybe that causes the problems?

     m_sheetView.ShowRowSelector = true;
                m_sheetView.SelectionUnit = SelectionUnit.Row;
                m_sheetView.OperationMode = OperationMode.ExtendedSelect;

  • Replied 8 September 2017, 2:40 pm EST

    The madness continues:

    not even that works:

     grid_Sheet1.RemoveRows(0,10);

    I am calling the Code in Question from a Contextmenustrip, the eventhandlers are wired up via delegates, maybe that's the problem?The code does get called (verified via Debugger).

    This is how i create my Contextmenu:

    var m = fpSpread.ContextMenuStrip = new ContextMenuStrip();
                m.Items.Add(new ToolStripMenuItem(@"DeleteRows(params int[] rows)", null, delegate { m_wrapper.DeleteRows(0, 8, 9, 4); }));
     

     

  • Replied 8 September 2017, 2:40 pm EST

    Hello,

    This code for removing Rows works fine at my end even with SelectionMode and OperationMode properties being set as you mentioned.

    In your first post you have talked about implementation of Custom ISheetDataModel, that could be an issue with your application wherein you are unable to get the Rows deleted from Spread. Could you please let me know how are you implementing this model and what all are you including in that. I would like to have look at the code for the same hence please send us a sample application replicating the same or the code for the implementation of Custom ISheetDataModel.

    Feel free to ask for further queries, we will assist you further. Thanks.

  • Replied 8 September 2017, 2:40 pm EST

    Hi and thanks for the reply,

    this is the Code for my Custom Model:

     

     public class BaseDataModel : ISheetDataModel
        {
    #pragma warning disable 0067
            public event SheetDataModelEventHandler Changed;
    #pragma warning restore 0067

            public string GetNote( int row, int column )
            {
                return null;
            }

            public object GetTag( int row, int column )
            {
                return null;
            }

            public bool IsEditable( int row, int column )
            {
                return true;
            }

            public bool IsEmpty()
            {
                return false;
            }

            public bool IsRowUsed( int row )
            {
                return true;
            }

            public void SetNote( int row, int column, string value ) { }
            public void SetTag( int row, int column, object value ) { }

            public virtual object GetValue( int row, int column )
            {
                throw new NotImplementedException();
            }

            public virtual void SetValue( int row, int column, object value )
            {
                throw new NotImplementedException();
            }

            public virtual int ColumnCount { get; set; }
            public virtual int RowCount { get; set; }
        }

        internal class ObjectListDataModel : BaseDataModel
        {
            private List> m_data;

            public ObjectListDataModel(int rows, int cols)
            {
                ColumnCount = cols;
                RowCount = rows;
               
               
                if(cols > 0 && rows > 0)
                {
                    m_data = new List>( rows );

                    for (int i = 0; i < rows; i++)
                    {
                        List row = new List(cols);
                        for (int j = 0; j < cols; j++)
                        {
                            row.Add(null);
                        }
                        m_data.Add(row);
                    }
                }
                else
                {
                    m_data = new List>();
                }
            }
           
            public override object GetValue( int row, int column )
            {
                if (m_data.Count > row)
                {
                    List lo = this.m_data[ row ];
                    if (lo.Count > column)
                    {
                        return lo[ column ];
                    }
                    return null;
                }
                return null;
            }

         
            public override void SetValue( int row, int column, object value )
            {
                if (m_data.Count > row)
                {
                    List lo = this.m_data[ row ];
                    if (lo.Count > column)
                    {
                        lo[ column ] = value;
                    }
                }
            }
           
        }

     

     

  • Replied 8 September 2017, 2:40 pm EST

    FLoibl,


    Your custom data model class is only implementing the ISheetDataModel interface.  This is the minimal interface needed for a sheet data model.  This interface only provides properties to determine size of sheet and methods to get/set values/notes/tags in sheet.


    There are several other interfaces that a sheet data model can implement to provide additional features.  The IRangeSupport interface provides methods to add/remove rows/column and clear/copy/move/swap range.  Your custom data model class needs to implement the IRangeSupport interface in order for the sheet to be able to remove rows from your custom data model.


    Bobby

  • Replied 8 September 2017, 2:40 pm EST

    Bobby,

    FWIW Instead of going the Interface way i now went with subclassing DefaultSheetDataModel and only overwrote SetValue and GetValue
    and that appears to work better, no exception etc.
    But in that variant i can't override RemoveRows..

     

  • Replied 8 September 2017, 2:40 pm EST

    Ok one last reply, i dumped my custom implementaion altogether in favor of DefaultSheetDataModel and everything works perfectly now :-)
    One question remains tough, i found this Thread from 2004 where somebody of your company talks about the internals of DefaultSheetDataModel:
    http://www.clubfarpoint.com/Forums/forums/thread/6824.aspx

    Is this still true? Or has the Impl of the DefaultSheetDataModel changed considerably since then?

  • Replied 8 September 2017, 2:40 pm EST

    Hello,

    DefaultSheetDataModel's implementation is still the same since then and its just that a new method has been added to the DefaultSheetDataModel class.

    Thanks.

  • Replied 8 September 2017, 2:40 pm EST

    Bobby,

    thanks a bunch for your reply, that was exactly the Information i needed.
    I got one more problem however:

    I have just two rows in a sheet and want to delete the second one,
    my custom RemoveRows runs but afterwards an Exception occurs because "IsRowExpanded" is called with an (now nonexistent) "row" Argument of "1".
    Maybe i have to invoke the "Changed" Event or sth?

    Are there any "roll your own data-model" examples available that cover more than the Basics in the ISheetDataModel?

  • Need extra support?

    Upgrade your support plan and get personal unlimited phone support with our customer engagement team

    Learn More

    Forum Channels