Maintain the selected record after upgrade Fill

Jair Souza

New member
Joined
Oct 3, 2013
Messages
3
Programming Experience
Beginner
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:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
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.
 

Jair Souza

New member
Joined
Oct 3, 2013
Messages
3
Programming Experience
Beginner
Sorry, but as I am beginner ... what exactly I need to change my code this?
You can show me how to do?
 

Jair Souza

New member
Joined
Oct 3, 2013
Messages
3
Programming Experience
Beginner
It worked well :

private void NoMoveReg()
{
try
{
int LinhaSelecionada = 0;

if (locatarioDataGridView.CurrentRow != null)

LinhaSelecionada = locatarioDataGridView.CurrentRow.Index;

this.locatarioTableAdapter.Fill(this.bDBiblioteca2DataSet.Locatario);

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

if (fotoTextBox.Text != "")

pictureBox2.ImageLocation = fotoTextBox.Text;

else

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