DataSource processing adds rows to the data grid

Posted by: tkrimer on 8 September 2017, 2:17 pm EST

  • Posted 8 September 2017, 2:17 pm EST

    I'm seeing a strange behaviour from the data grid.


    I have a databound grid with some additional empty rows.


    If after a batch editing on the data source, I delete an entire row from the grid and than click on another cell in data grid, a new row is added to the bottom of the grid. 


    Attached is a sample project that reproduces the "bug".


    To reproduce do the following:


    1. click on "batch edit on data source" button


    2. select an entire row


    3. click on "delete selected" button


    4. click on some cell


    Result: row is added at the bottom


     


    2010/04/addedRowBugReproduced.zip
  • Replied 8 September 2017, 2:17 pm EST

    I have 2 user requirements:


    1. for the grid to have additional empty rows like in Excel


    2. the ability to add bond rows.


    This is why I set the :


    fpSpread1_Sheet1.DataAllowAddNew = true;


    fpSpread1_Sheet1.RowCount = 100;


    If this is the behaviour I want to acheive, what should I do?


    But in any case, the behaviour in the sample project i attached is strange in any case.


    Did you manage to reproduce it? All I was doing is batch editing on the data source, then deleting a row from the grid and then the grid adds a row automatically, whitout my request.


    I understand that the Spread will add the row automatically when the row is edited, but in my senario I wasn't editing the row, it is being added automatically by the Spread when I click on any other cell of the grid.


    To make it more clear:


    1. click on "batch edit..."


    2. select row "5"


    3. click on "delete selected"


    4. click on cell (5,0)


    Result: row 14 is added autojmatically by the Spread, without editing it, without requesting it....


    Please, tell me if you were able to reproduce.


     

  • Replied 8 September 2017, 2:17 pm EST

    Hi,

    That behavior (adding the new unbound row at the bottom automatically) is the designed behavior of DataAllowAddNew.  It is intended to:

    1. Add a new unbound row at the bottom of your sheet, and put a * in the row header like DataGridView does to indicate the row is new and not yet added to the data source.

    2. Add the unbound row at the bottom to the data source when it is changed (the * goes away), then add a new unbound row at the end.

    If you don't want that behavior, then you should not set DataAllowAddNew to true.  In that case, you can still add unbound rows at the bottom, but it will not put the * in those row headers, and it will not automatically add those rows when they are edited.  You can use code to add them by calling the AddUnboundRows method.

  • Replied 8 September 2017, 2:17 pm EST

    As I described I want the behaviour of :


    1. Additional empty rows


    2. Ability to add new bound row.


    If I turn off the DataAllowAddNew I can't add new bound rows through grid. So again, how would I acheive this? Can you write a sample?


    And again "That behavior (adding the new unbound row at the bottom automatically) is the designed behavior of DataAllowAddNew" only when I start editing this new row, but I don't edit it at all. You can reproduce it with the sample I wrote.

  • Replied 8 September 2017, 2:17 pm EST

    Hi,

    DataAllowAddNew will add a new unbound row at the bottom of the sheet.  Did you intend to use that property (the behavior you describe is the designed behavior for it)?  I see you are setting it to true in the code you posted.

    You do not need to use DataAllowAddNew to add unbound rows at the bottom of the sheet, but if you want the unbound rows to get added to your data source, you either need to set DataAllowAddNew to true (and Spread will add them automatically when they are edited), or you need to call AddUnboundRows in your code to add them.

  • Replied 8 September 2017, 2:17 pm EST

    Hi,

    I do see the behavior where it adds the unbound row automatically in your sample, and it does indeed look like a bug to me.  I would need to forward it to the development team for further investigation to determine why that is happening and whether it can be fixed in a future release.  However, if you comment out the line of code in your sample that sets RowCount to 100, then you will see the intended behavior of DataAllowAddNew (with only one extra row at the bottom) and I wonder if that would meet your requirement without having to wait for a fix.  I will forward your sample to the developers anyway.

    If you need to have that many extra unbound rows at the bottom, you can achieve that without setting DataAllowAddNew to true.  You can remove the code setting DataAllowAddNew to true, and add some code to handle the LeaveCell event to check whether the user is leaving an unbound row and moving to a different row, and prompt the user to add the row (or simple add it):

                //fpSpread1_Sheet1.DataAllowAddNew = true;
                fpSpread1_Sheet1.RowCount = 100;
                fpSpread1.LeaveCell += new FarPoint.Win.Spread.LeaveCellEventHandler(fpSpread1_LeaveCell);
    ...

            void fpSpread1_LeaveCell(object sender, FarPoint.Win.Spread.LeaveCellEventArgs e)
            {
              if (e.Row != e.NewRow && fpSpread1_Sheet1.Equals(fpSpread1.ActiveSheet) && !fpSpread1_Sheet1.IsRowBound(e.Row) && DialogResult.Yes == MessageBox.Show("Do you want to add this row to the data source?", "Add new row?", MessageBoxButtons.YesNo))
                fpSpread1_Sheet1.AddRowToDataSource(e.Row, true);
            }

  • Replied 8 September 2017, 2:17 pm EST

    Yes. Thank you.


    I eventually made a very similar workaround:


    //fpSpread1_Sheet1.DataAllowAddNew = true;


    fpSpread1_Sheet1.RowCount = 100;


    fpSpread1_Sheet1.Models.Data.Changed += new SheetDataModelEventHandler(Data_Changed);


    ......


    void Data_Changed(object sender, SheetDataModelEventArgs e)


    {


    if (e.Type == SheetDataModelEventType.CellsUpdated)


    {


    if (e.Row >= sr.students.Count)


    {


    // new row


    fpSpread1_Sheet1.AddRowToDataSource(sr.students.Count, true);


    fpSpread1_Sheet1.AddUnboundRows(fpSpread1_Sheet1.RowCount, 1);


    }


    }


    }

Need extra support?

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

Learn More

Forum Channels