如何在ASP.NET中的GridView中编辑,更新和删除行

时间:2013-08-04 17:12:18

标签: c# asp.net sql-server gridview ado.net

我正在从Database检索数据到GridView。 我不知道Edit中的DeleteGridView行以及UpdateDatabase的行<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 。 如果我们的代码中有任何错误,请告诉我

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;

}

我的后端代码是

{{1}}

}

1 个答案:

答案 0 :(得分: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;
            }
        }
        }

更新 - &gt;

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();
}
}
}

删除 - &gt;&gt;

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();
}
}
}

在按钮的每个事件之后,您可以创建一个BindGrid ...从数据网格刷新您的数据...在BindGrid方法中,您需要重新制作刚刚执行的select方法...如果您有问题告诉我