Cómo Editar,Actualizar y Eliminar Filas en GridView en ASP.NET

Estoy recuperando Datos De Database en GridView.
No sé cómo Edit y Delete fila en GridView y También Update en Database.
También por favor decirme si hay algún error en mi Código

<head runat="server">
<title></title>
<style type="text/css">
    .style1
    {
        width: 248px;
    }
    .style2
    {
        width: 100%;
    }
    .style3
    {
        height: 180px;
    }
</style>
 </head>
 <body>
<form id="form1" runat="server">
<div class="style3">
<h1 align="center">Students Personal Information
</h1>
    <table class="style2">
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
    <asp:Button ID="Button1" runat="server" Text="Insert Data" 
        onclick="Button1_Click" />
            </td>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
        Text="Show All Students" Width="128px" />
            </td>
        </tr>
    </table>

    &nbsp;<br />
    <br />
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <br />
    <br />

    <br />
    <br />
</div>
<br />
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ButtonType="Button" ShowEditButton="True" />
    </Columns>
</asp:GridView>
</form>
 </body>
   </html>

enter code here

Y mi código back-end es

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

  public partial class _Default : System.Web.UI.Page
{
 SqlConnection conn = new SqlConnection("Data Source=DATA_NET_81_SOF;Initial                
 Catalog=Students;Integrated Security=True");     
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Label1.Text = "Student's Name";
        Label2.Text = "Student's Class";
        Label3.Text = "Student's Roll Number";
    }

}
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        SqlCommand cmd = new SqlCommand("Insert INTO Personalinfo(StudentName,StudentClass,StudentRollNo)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", conn);
        conn.Open();
        cmd.Parameters.AddWithValue("StudentName", TextBox1.Text);

        cmd.Parameters.AddWithValue("StudentClass", TextBox2.Text);

        cmd.Parameters.AddWithValue("StudentRollno", TextBox3.Text);

        cmd.ExecuteNonQuery();
        Label4.Text = "Data Is Stored";
    }
    catch (Exception ex)
    {
        Label4.Text = ex.Message;
    }

}


protected void Button2_Click(object sender, EventArgs e)
{
    SqlCommand sql = new SqlCommand("Select * from Personalinfo", conn);
    SqlDataAdapter da = new SqlDataAdapter(sql);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = (ds);
    GridView1.DataBind();


}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    e.Cancel = true;
    GridView1.EditIndex = -1;

}

}

InformationsquelleAutor Saad Lakhany | 2013-08-04

1 Kommentar

  1. 1
    private string connection = @"...";
    
      protected void Button1_Click(object sender, EventArgs e)
            {
             using(SqlConnection con = new SqlConnection(connection))
        {
                try
                {
                    SqlCommand cmd = new SqlCommand("Insert INTO Personalinfo(StudentName,StudentClass,StudentRollNo)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
                    con.Open();
    
                    cmd.ExecuteNonQuery();
                    Label4.Text = "Data Is Stored";
                }
                catch (Exception ex)
                {
                    Label4.Text = ex.Message;
                }
            }
            }

    Para Actualizar –>

    protected void Button_Update(object sender, EventArgs e){
     using(SqlConnection con = new SqlConnection(conn))
    {
      using(SqlCommand cmd = new SqlCommand())
    {
      cmd.Connection = con;
      cmd.CommandText = "UPDATE Personalinfo SET StudentName = @1 ... WHERE Student_Id= @N";
      cmd.Parameters.Add("@1",SqlDbType.NVarChar).Value = your_value;
      cmd.Para.....
      cmd.Parameters.Add("@N",.....).Value = your_student_id;
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
    }
    }
    }

    Para Eliminar –>>

    protected void Button_Delete(object sender, EventArgs e){
     using(SqlConnection con = new SqlConnection(conn))
    {
      using(SqlCommand cmd = new SqlCommand())
    {
      cmd.Connection = con;
      cmd.CommandText = "DELETE FROM Personalinfo WHERE StudentName = '"+TextBox1.Text+"'";
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
    }
    }
    }

    Después de cada evento de botón, usted puede hacer una BindGrid … para actualizar sus datos de la cuadrícula de datos… en BindGrid método que usted necesita para volver a seleccionar el método que acabo de hacer … si tienes problema me dicen

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein

Pruebas en línea