Project disaster: updating the info in a DataGrid without moving the VSB

When I run dataGrid.Update(); the vertical scroll bar is always moved to the top. How do you update a datagrid keeping the scroll bar in the same position



Answer this question

Project disaster: updating the info in a DataGrid without moving the VSB

  • Suja

    Then don't clear it, it should update changed rows based on primary key just fine.

    The potential issue with that is what deleted rows (as in deleted from database) won’t be removed from dataset.

    If that’s possible in your scenario, you might have to get data into another dataset and merge them.



  • isunshine

    Does a NETCF 2.0 DataGrid support list notifications The datagrid is not updated if I don't call Update(); manually. This is my program: (parts of it)

    private DataTable tableAMAL;
    tableAMAL = new DataTable();
    dataGridAMAL.DataSource = tableAMAL;
    dataAdapterAMAL = new MimerDataAdapter(selectSql, con);
    dataAdapterAMAL.Fill(tableAMAL);
    dataGridAMAL.TableStyles.Clear();
    dataGridAMAL.TableStyles.Add(ts);
    dataGridAMAL.Width = dgcsAlarmName.Width + dgcsStateChangedTo.Width + dgcsTimestamp.Width + dgcsAck.Width + 38;

    Why isn't the datagrid updated automatically when the data in the database changes


  • Nightowly

    When I do this on the PC:

    DataSet ds2 = new DataSet();
    mimerDataAdapter.Fill(ds2, globalTableName);
    dv = new DataView(ds2.Tables[globalTableName]);
    dv.Sort = "Id";
    dataGrid.DataSource = dv;

    It doesn't scroll. But when I do the same thing on the Win CE device it scrolls to the top. Could you outline in more detail what you mean I should do

    /Lars


  • kymaita

    Merge does not require that information as you can compare rows in old data set and new dataset based on primary key.

    For example:

    - If new dataset has row, but old does not, that means row has been added - copy row from new to old.

    - If old dataset has row with some primary key, but new does not, that means row has been deleted - delete row from old.

    - If row's in both and not marked Modified in old dataset, replace row in old with new one in case it was changed.

    - If row's in both and marked Modified in old dataset, you have a concurrency conflict which needs to be resolved as appropriate for your application.



  • EricLeBouffon

    That is strange, it should reset everything even on desktop as you're rebinding. That might be a bug, so I would not count on desktop "not scrolling" in future versions because it should reset bindings for new data source.

    What I suggested you do, just comment out the first line here, do not rebind.

    // ds.Tables[globalTableName].Clear();
    mimerDataAdapter.Fill(ds, globalTableName);

    If you can delete records (and you care for them to be removed from local data set), look into DataTable.Merge on MSDN.



  • MarkBosley

    That's the way DataGrid is updated if you use data source with change notification (e.g. DataTable). As soon as record is changed in the data source, it's reflected by the grid automatically.

    If you're using data source without change notification, you'd need to rebind which would of course reset everything including current row which would move SB to first row.



  • rp1933

    Yes, DataGrid on NETCF uses these notifications; it's up to the data source to support them. DataGrid would reflect changes made to DataTable bound to it, but it won't show anything if you change your data base directly. DataTable not even aware of data base existence so it has no idea you're changing the database. By the way, desktop works in exactly the same way.

    As to code you've posted, it's missing the most important part - changing the data. It looks good to me assuming you're executing it only ones as it would naturally reset all bindings.



  • xRuntime

    The scroll bar moves so the row with the current row index is positioned at the very top of the datagrid. If the user scrolls down and doesn't click a row every now and then the scrollbar will scroll to the current row index row every 5 seconds. That's not acceptable. I'll have to edit the datatables manually so the scrollbar doesn't jump around.


  • Blast

    Hi,

    have you tried with the properties CurrentCell or CurrentRowIndex, getting the value before updating and resetting it afterwards


  • inzel

    Is there a way to update the info in a DataGrid control without moving the vertical scroll bar Otherwise it will be a disaster for our project. (In C#)



  • chaza

    I understand the merge part. But when I fill my second dataset (rows will be deleted and have to be removed) there wont be any pending changes in that dataset or datatable for that matter. In the examples at MSDN the alter and add rows and thereby get datasets with changes which hasn't already been accepted. How do you get a dataset with pending changes when you use Fill to get all data in the table from the database
  • erodcav

    I see. But how do you update the datasource without scrolling the VSB to the top. I tried:

    ds.Tables[globalTableName].Clear();
    mimerDataAdapter.Fill(ds, globalTableName);

    (This is from Mimer's ADO.NET example.)
    But when the dataTable was cleared it scrolled to the top. If you fill it twice without clearing it it stays in the same position.

    What happened to http://www.alexfeinman.com/dowload.asp doc=ScrollGrid.zip I could use that to scroll to a certain row, I think.


  • Project disaster: updating the info in a DataGrid without moving the VSB