DataRow index issue

Posted by: atos-38 on 8 September 2017, 12:59 pm EST

  • Posted 8 September 2017, 12:59 pm EST

    Hello,

    I am having trouble getting the right datarow index from a dataset bound to a sheet.
    I read several posts and don't find where I am doing wrong.

    Here's an example :

          DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("COL1");
    dt.Columns.Add("COL2");
    dt.Columns.Add("COL3");
    dt.Rows.Add(new object[] { "value0_0", "value0_1", "value0_2" });
    ds.Tables.Add(dt);
    ds.AcceptChanges();

    fpSpread1_Sheet1.DataSource = ds;

    int view_row = 0;
    int model_row = fpSpread1_Sheet1.GetModelRowFromViewRow(view_row); // Equals 0
    int data_row = ((FarPoint.Win.Spread.Model.DefaultSheetDataModel)fpSpread1_Sheet1.Models.Data).GetDataRowFromModelRow(model_row); // Equals 0

    fpSpread1_Sheet1.Rows[0].Remove();
    fpSpread1_Sheet1.AddRows(0, 1);

    view_row = 0;
    model_row = fpSpread1_Sheet1.GetModelRowFromViewRow(view_row); // Equals 0
    data_row = ((FarPoint.Win.Spread.Model.DefaultSheetDataModel)fpSpread1_Sheet1.Models.Data).GetDataRowFromModelRow(model_row); // Equals 0

    ((DataSet)fpSpread1_Sheet1.DataSource).Tables[0].Rows[data_row][0] = "test";


     I am having a System.Data.DeletedRowInaccessibleException when trying to access the data.
    The dataset has 2 rows, the first one is the deleted one and the second one is the row that has been added. The GetDataRowFromModelRow(model_row) method should give me 1 and not 0 or am I missing something ?
     
  • Replied 8 September 2017, 12:59 pm EST

    Your dataset only has one row, not two.  You are then deleting that row and adding one row to the spread.  You are receiving the exception on the last line because the dataset hasn't been notified of the newly added row.  You need to call AcceptChanges again after adding the new row to update the dataset.  Then your last line of code will work...

    int view_row = 0;
    int model_row = fpSpread1_Sheet1.GetModelRowFromViewRow(view_row); // Equals 0
    int data_row = ((FarPoint.Win.Spread.Model.DefaultSheetDataModel)fpSpread1_Sheet1.Models.Data).GetDataRowFromModelRow(model_row);

    fpSpread1_Sheet1.Rows[0].Remove();
    fpSpread1_Sheet1.AddRows(0, 1);

    view_row = 0;
    model_row = fpSpread1_Sheet1.GetModelRowFromViewRow(view_row); // Equals 0
    data_row = ((FarPoint.Win.Spread.Model.DefaultSheetDataModel)fpSpread1_Sheet1.Models.Data).GetDataRowFromModelRow(model_row); // Equals 0

    ds.AcceptChanges();

    ((DataSet)fpSpread1_Sheet1.DataSource).Tables[0].Rows[data_row][0] = "test";

  • Replied 8 September 2017, 12:59 pm EST

    At the end, the dataset has 2 rows, it has been notified of the newly added row as the first one is the deleted one and the second is the new one.
    I don't want to call the AcceptChanges method as I want to keep a trace of what the user does until he saves his datas.
  • Replied 8 September 2017, 12:59 pm EST

    Hello,


    Your last line of code is incorrect. You are assuming the GetDataRowFromModelRow is returning a row index from the DataSource and it is not. It is returning a row from the DataView that is bound to Spread. You can get this DataView by calling the GetDataView method of the SheetView object. Then you can use the index to get the correct DataRowView object. And this has a Row property to return the underlying DataRow object from the DataSource. Here is the updated last line of code.

     
          fpSpread1_Sheet1.GetDataView(true)[data_row].Row[0] = "test";
     
Need extra support?

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

Learn More

Forum Channels