I have this code that works when I select a single Row and enters them correctly where they go, now the issue is that I have to select 1x1, there is a way to choose the first row and then action is repeated for the other 5 below it In the order they are? As you can see, each one has different textboxes, or if there is a guide or YouTube video where I could gather information on how to do it or whether or not I will always have to do 1x1
int rowCount = dataGridView2.RowCount; // Obtener el número total de filas en el DataGridView
if (rowCount >= 6)
{
int selectedRowIndex = dataGridView2.CurrentCell.RowIndex; // Obtener la fila actualmente seleccionada
if (selectedRowIndex >= 0 && selectedRowIndex < 6)
{
if (selectedRowIndex == 0)
{
txtDniEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
txtApellido2Esposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtApellidoEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
txtNombreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
txtPaisEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[8].Value.ToString();
}
else if (selectedRowIndex == 1)
{
txtDniEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
txtApellido2Esposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtApellidoEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
txtNombreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
txtPaisEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[8].Value.ToString();
}
else if (selectedRowIndex == 2)
{
txtDniMadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
txtApellidoMadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
txtApellido2MadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtNombreMadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
}
else if (selectedRowIndex == 3)
{
txtDniPadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
txtApellidoPadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
txtApellido2PadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtNombreMadreEsposa.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
}
else if (selectedRowIndex == 4)
{
txtDniPadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
txtApellidoPadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
txtApellido2PadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtNombrePadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
}
else if (selectedRowIndex == 5)
{
txtDniMadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
textBox3.Text = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
textBox4.Text = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
txtMadreEsposo.Text = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
}
// Realiza aquí las operaciones que necesites con los datos asignados a los TextBox
}
else
{
}
enter image description here i Expect when i do 1 click in Cargar Datos Casamiento to get all the info and not going 1x1
The code can be significantly simplified for perception as follows:
if (selectedRowIndex >= 0 && selectedRowIndex < 6)
{
var name1 = dataGridView2.Rows[selectedRowIndex].Cells[4].Value.ToString();
var name2 = dataGridView2.Rows[selectedRowIndex].Cells[2].Value.ToString();
var name3 = dataGridView2.Rows[selectedRowIndex].Cells[1].Value.ToString();
var name4 = dataGridView2.Rows[selectedRowIndex].Cells[0].Value.ToString();
var name5 = dataGridView2.Rows[selectedRowIndex].Cells[8].Value.ToString();
if (selectedRowIndex == 0)
{
txtDniEsposo.Text = name1;
txtApellido2Esposo.Text = name2;
txtApellidoEsposo.Text = name3;
txtNombreEsposo.Text = name4;
txtPaisEsposo.Text = name5;
}
else if (selectedRowIndex == 1)
{
txtDniEsposa.Text = name1;
txtApellido2Esposa.Text = name2;
txtApellidoEsposa.Text = name3;
txtNombreEsposa.Text = name4;
txtPaisEsposa.Text = name5;
}
...
Of course, instead of name1
, name2
, give the variables descriptive names.
You can shorten the code even more:
var cells = dataGridView2.Rows[selectedRowIndex].Cells;
var name1 = cells[4].Value.ToString();
var name2 = cells[2].Value.ToString();
var name3 = cells[1].Value.ToString();
var name4 = cells[0].Value.ToString();
var name5 = cells[8].Value.ToString();