none
Editar Registros de un gridview

    Pergunta

  • Hola, buenas tardes!

     

    Estoy tratando de editar los registros de un Gridview.

    El Grid lo lleno con un SqlDataSource que tiene como SelectCommandType un Store Procedure ("StoreLlena")

     

    En el evento RowUpdating DEL GRID me traje los datos del Grid que hay que actualizar

    Label LblId = (Label)GridView2.Rows[GridView2.EditIndex].FindControl("label1");

    DropDownList ddldat = GridView2.Rows[e.RowIndex].FindControl("cmb1") as DropDownList;

     

    String query = String.Empty;

            query = "StoreActualiza";

     

            conexion.Open();

            SqlCommand cmd = new SqlCommand(query, conexion);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@CAMPO1", LblId.Text);

            cmd.Parameters.AddWithValue("@CAMPO2", ddldat.SelectedItem.Text);

            cmd.ExecuteNonQuery();

            conexion.Close();

            GridView2.EditIndex = -1;

            GridView2.DataBind();

     

    Me marca el siguiente error

    Updating is not supported by data source "StoreLlena"

    unless updatecommand is specified

     

     

    Alguien sabe como lo puedo solucionar? alguna sugerencia?

     

    Muchas Gracias de Antemano.

    • Movido Rudedog2MVP sábado, 30 de julho de 2011 13:16 foreign language post (From:Visual C# General)
    sexta-feira, 29 de julho de 2011 18:40

Respostas

  • Usted debe tener un adaptador de datos por este ejemplo.

     private void BindGridView()
    
      { 
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(GetConnectionString()); 
        try {
    
          connection.Open();
          string sqlStatement = "SELECT Top(10)* FROM Customers";
          SqlCommand cmd = new SqlCommand(sqlStatement, connection);
          SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
           sqlDa.Fill(dt);
    
           if (dt.Rows.Count > 0)
           {
    
            GridView1.DataSource = dt;
            GridView1.DataBind();
           }
        }
        catch (System.Data.SqlClient.SqlException ex)
    
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
          connection.Close();
        }
    
    
    Lea sobre dataadpaters ado.net


    JP
    • Marcado como Resposta mayra.perezl terça-feira, 2 de agosto de 2011 04:00
    sexta-feira, 29 de julho de 2011 22:50

Todas as Respostas

  • Translated from "Google translate":

    Hello, good afternoon!
    I'm trying to edit records in a Gridview.
    I fill the grid with SqlDataSource which is SelectCommandType a Store Procedure ("StoreLlena")
     

    In the event I brought RowUpdating THE GRID Grid data to be updated
    LblId label = (Label) GridView2.Rows [GridView2.EditIndex]. FindControl ("label1");
    Ddldat DropDownList = GridView2.Rows [e.RowIndex]. FindControl ("cm -1") as DropDownList;
     

    String query = String.Empty;
             query = "StoreActualiza"
             conexion.Open ();
             SqlCommand cmd = new SqlCommand (query, conn);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue ("@ Field1", LblId.Text);
             cmd.Parameters.AddWithValue ("@ FIELD2" ddldat.SelectedItem.Text);
             cmd.ExecuteNonQuery ();
             connection.close ();
             GridView2.EditIndex = -1;
             GridView2.DataBind ();

    I mark the following error
    Updating is not supported by data source "StoreLlena"
    unless UpdateCommand is Specified

    Anyone know how I can fix this? any suggestions?

    Thank you very much beforehand.


    John Grove, MCC - Senior Software Engineer
    sexta-feira, 29 de julho de 2011 19:11
  • Usted debe preguntar a sus preguntas en el foro de Windows Forms y DataBinding. Creo que estos son foros de Inglés sin embargo.

    http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/threads


    John Grove, MCC - Senior Software Engineer
    sexta-feira, 29 de julho de 2011 19:14
  • sexta-feira, 29 de julho de 2011 22:43
  • Usted debe tener un adaptador de datos por este ejemplo.

     private void BindGridView()
    
      { 
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(GetConnectionString()); 
        try {
    
          connection.Open();
          string sqlStatement = "SELECT Top(10)* FROM Customers";
          SqlCommand cmd = new SqlCommand(sqlStatement, connection);
          SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
           sqlDa.Fill(dt);
    
           if (dt.Rows.Count > 0)
           {
    
            GridView1.DataSource = dt;
            GridView1.DataBind();
           }
        }
        catch (System.Data.SqlClient.SqlException ex)
    
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
          connection.Close();
        }
    
    
    Lea sobre dataadpaters ado.net


    JP
    • Marcado como Resposta mayra.perezl terça-feira, 2 de agosto de 2011 04:00
    sexta-feira, 29 de julho de 2011 22:50
  • Muchas Gracias por sus respuestas. Me han ayudado bastante

    Saludos

     


    Mayra
    terça-feira, 2 de agosto de 2011 04:00