There is a DataGridView dgv ,

  dgv.DataSource = myDateTable; 

There is a line selection code

 private static void SelectRowByOrderID(DataGridView dgv, string orderID) { dgv.Rows .OfType<DataGridViewRow>() .Where(x => (string)x.Cells["#"].Value == orderID) .ToArray<DataGridViewRow>()[0] .Selected = true; } 

Actions:

  • select row X;
  • open the edit form;
  • save changes in the database;
  • close the edit form;
  • update myDateTable table
  • update dgv.DataSource

Question:

  • how after dgv update select line x?
  • What is the problem to cause this method after updating dgv ? - default locale
  • I do not know what event to register - Aleksandr H.
  • And where do you update dgv.DataSource ? Why is it impossible to immediately select lines there? - default locale
  • @AleksandrH. how do you update dgv? Directly dgv or through changes myDateTable? - Arantler
  • @Arantler, through the change myDateTable (read from OleDbDataReader ), is not an ObservalableCollection . At the moment, I decided this way: after the editing form is closed, the method is called, which reads the data from the database again, draws the grid, moves the cursor to the X line. Now the question is: how not only to move the cursor to the X line, but also to restore the vertical level after the update scrolled. - Aleksandr H.

1 answer 1

If you update the dgv through the changes DataSource И if myDateTable is ObservalableCollection И

then you are interested in ObservalableCollection.CollectionChanged enter image description here

Subscribe to the event and select what you need. Or, if you are not satisfied with the usual event, then create your own and subscribe to it.