Load data in Textbox from Gridview

0

I want to load data from a row that when I select it by means of a button, I upload it in a format. For this I am doing it with the CommandName, as if they were coordinates, if you click on such a row it will load this data and so on for all. But I do not know how to do it in rows and cells with textbox.

   if (e.CommandName.ToString() == "clic") {

       int indexrow = int.Parse(e.CommandArgument.ToString());
       int id = (int)this.gvbuscar.DataKeys[indexrow]["Id"];
    
asked by KlonDTS 15.12.2016 в 20:07
source

2 answers

1

To be able to assign the fields from DataGridView to TextBox you have to do it in the following way:

Creating a function that fills the TextBox

public static void LlenarTextBox(int IndiceDGV)
{
   TextBox1.Text = DataGridView1.Rows[IndiceDGV].Cells[0].Value.ToString(); //TextBox1 = Ap.Paterno
   TextBox2.Text = DataGridView1.Rows[IndiceDGV].Cells[1].Value.ToString(); //TextBox2 = Ap.Materno
   TextBox3.Text = DataGridView1.Rows[IndiceDGV].Cells[2].Value.ToString(); //TextBox3 = Nombre
}

IndiceDGV would be the current index of the row you want to select in DataGridView . Then the function is called in the event Click of each button in the following way:

private void Button1_Click(object sender, EventArgs e)
{
    LlenarTextBox(0);
}

In this case, the zero ( 0 ) is sent indicating the first row.

To make the code more homogeneous, you could do the following:

private void Button1_Click(object sender, EventArgs e)
{
    LlenarTextBox(DataGridView1.CurrentRow.Index);
}

But I'm not sure if clicking on the button this property is full.

    
answered by 07.06.2017 в 18:37
0

To select a record in the gridview you have to use the DataKeyNames and DataKeys

[ASP.NET] GridView - Employee Edition

Analyze in the article:

  • How the grid is defined the name of the field that identifies the entity in the DataKeyNames

  • The definition of CommandName="Select" to run the SelectedIndexChanging event

The way to take the selected value

int id = Convert.ToInt32(NombreGrid.DataKeys[e.NewSelectedIndex].Value);

with this you can redirect to another screen to edit the entity

    
answered by 15.12.2016 в 22:28