Maintain the selected record after upgrade Fill

Jair Souza

New member
Oct 3, 2013
Programming Experience
Hello, I have a Windows Form which has a tabcontrol page1 with the format Details "Register" and page2 GridView "Consultation".
The form starts on page1, the BindingNavigator created the Change button that enables the textbox to be changed ... after that I have the option
Save or cancel. The issue is that saving or canceling it moves to the first record, regardless of the option must
He chosen to remain in the same record. How to do this?

I set up this code, but when you open the form you need to go in TabPage2 where the grid, to work ... if it gives the following error
"Specified argument was out of the range of valid values​​. Parameter name: value."

what needs to change to work without going on the grid?

* Remember that I'm totally beginner, I got this code doing research and adaptations.

Privaite nomove void ()

linhaSelecionada int = 0 primeiraLinha = 0;

if (dataGridView.CurrentRow! = null)
primeiraLinha = dataGridView.FirstDisplayedScrollingRowIndex;
linhaSelecionada = dataGridView.CurrentRow.Index;
this.locatarioTableAdapter.Fill (this.bDBiblioteca2DataSet.Locatario);

dataGridView.FirstDisplayedScrollingRowIndex = primeiraLinha;

DataGridView.Rows [linhaSelecionada]. Selected = true;
dataGridView.CurrentCell DataGridView.Rows = [linhaSelecionada]. Cells [1];

I appreciate if you can help.
Last edited:
Why are you calling Fill at all? Once you call Fill at the start, there's no need to call Fill again. If you want to mnake changes to the data then you make changes to the data in the DataTable. You then call Update on the same data adapter to save those changes back to the database. DO NOT change the database first and then retrieve all the data again. Retrieve the data ONCE ONLY, make all your changes to that data first, then save those changes to the database.
It worked well :

private void NoMoveReg()
int LinhaSelecionada = 0;

if (locatarioDataGridView.CurrentRow != null)

LinhaSelecionada = locatarioDataGridView.CurrentRow.Index;


locatarioDataGridView.CurrentCell = locatarioDataGridView.Rows[LinhaSelecionada].Cells[0];

if (fotoTextBox.Text != "")

pictureBox2.ImageLocation = fotoTextBox.Text;


pictureBox2.ImageLocation = Application.StartupPath.ToString() + "\\FotosLocatarios\\" + "SemFoto.png";
catch (Exception ex)
MessageBox.Show("Ocorreu um Erro !" + ex.Message, "Aten??o", MessageBoxButtons.OK, MessageBoxIcon.Error);

And the load :

locatarioDataGridView.FirstDisplayedScrollingRowIndex = locatarioDataGridView.SelectedRows[0].Index;
Top Bottom