none
Editar Registros de un gridview RRS feed

  • Question

  • 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.

    • Moved by Rudedog2 Saturday, July 30, 2011 1:16 PM foreign language post (From:Visual C# General)
    Friday, July 29, 2011 6:40 PM

Answers

  • 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
    • Marked as answer by mayra.perezl Tuesday, August 2, 2011 4:00 AM
    Friday, July 29, 2011 10:50 PM

All replies

  • 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
    Friday, July 29, 2011 7:11 PM
  • 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
    Friday, July 29, 2011 7:14 PM
  • Friday, July 29, 2011 10:43 PM
  • 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
    • Marked as answer by mayra.perezl Tuesday, August 2, 2011 4:00 AM
    Friday, July 29, 2011 10:50 PM
  • Muchas Gracias por sus respuestas. Me han ayudado bastante

    Saludos

     


    Mayra
    Tuesday, August 2, 2011 4:00 AM