2011-12-09 21 views
6

En mi página ASP.NET, estoy usando un GridView para ver los datos (Artículos & sus precios). Actualmente, los usuarios pueden editar los datos (precios) en la cuadrícula fila por fila. (Haga clic en el enlace "Editar", cambie los valores y luego "Actualizar"). Esto es ROW por ROW. ¿Es posible abrir todas las filas en el modo Editar & con un solo botón (por ejemplo, Enviar) para actualizar todos los datos una vez?ASP GridView Todas las Filas En el Modo de Edición

Respuesta

5

Si no necesita leer solo el modo, en ese caso puede colocar cuadros de entrada (cuadro de texto, lista desplegable, etc.) en la sección ItemTEmplate y vincularlos con los datos existentes.

A continuación, coloque un botón de envío en la parte superior/inferior del evento GridView y maneje el botón Click y recorra los datos del elemento GridView y guarde toda la base de datos.

Voy a publicar el bloque de código si es necesario. Gracias por tu tiempo.

+0

Estoy teniendo el mismo problema para mi gridview. ¿Es posible que publique el código para el evento de clic de botón? – developthestars

0

Y tendrá un mejor control sobre eso haciendo uso de listview en lugar de gridview.

Mi práctica recomendada es usar listview y el control de usuario web personalizado para este tipo de problemas.

Si completa su lista de lista con su control de usuario, será fácil administrar su método de guardado, solo tiene que repetir los elementos de la lista, encontrar el control y llamar a su método Save para cada artículo.

0

Sé que esta pregunta ya ha sido contestada, pero aquí es el código de bucle a través del GridView la obtención de datos y almacenarlo en la Base de Datos:

Uso de bibliotecas:

  • utilizando System.Data ;
  • usando System.Data.SqlClient;
  • usando System.Web.Configuration;
  • usando System.Data.Odbc;

código subyacente:

// this is a variable that have the Query or SQL Commands. 
string DataBaseQuery = "UPDATE [table] SET [variable2] = @variable2, [variable3] = @variable3) WHERE [variable1] = @variable1"; 

//Click Event from a LinkButton. 
protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
    //"ConnectionString" its the string connection for your DataBase (often get from the WebConfig File or a DataSource element. 
    using (SqlConnection connection = new SqlConnection(ConnectionString)) 
    { 
    //this is for open the database using the string connection. 
    connection.Open(); 

    //this is the algorithm for going through the entire GridView. 
    for (int i = 0; i < GridView1.Rows.Count; i++) 
    { 
     //"DataBaseQuery" it's a string variable that have the Query or SQL Commands. 
     SqlCommand cmd = new SqlCommand(DataBaseQuery, conexion); 

     //this case it's for obtain the text variable of the first column of the gridview (in my case it was the ID of the register). 
     cmd.Parameters.AddWithValue("@variable1", ((Label)GridView1.Rows[i].Cells[0].FindControl("Label1")).Text.ToString()); 

     //this case it's for obtain the selected value of a DropDownList that were in the 14 th column) 
     cmd.Parameters.AddWithValue("@variable2", ((DropDownList)GridView1.Rows[i].Cells[15].FindControl("DropDownlist2")).SelectedValue.ToString()); 

     //this command it's for obtain the text of a textbox that is in the 15 th column of the gridview. 
     cmd.Parameters.AddWithValue("@variable3", ((TextBox)GridView1.Rows[i].Cells[16].FindControl("TextBox17")).Text.ToString()); 

     cmd.ExecuteNonQuery(); 
    } 

    //after going through all the gridview you have to close the connection to the DataBase. 
    connection.Close(); 
    } 
} 

Por supuesto, usted tiene que ajustar el código a su caso particular, pero es muy fácil. En este código tiene el ejemplo para obtener valores para otro objeto como labes, textbox y dropdownlist en gridview.

Sufrí mucho para ejecutar este código (no soy buena programación) pero estoy feliz de ayudar.

NOTA: Para contar las columnas de la vista de cuadrícula debe comenzar en cero. NOTA 2: Lo siento por mi mal inglés por cierto ... No es mi lenguaje natural.

Cuestiones relacionadas