2013-08-04 186 views
3

我正在從Database檢索數據到GridView。 我不知道如何EditDelete行在GridView它也UpdateDatabase。 也請告訴我,如果他們是在我的代碼中的任何錯誤如何編輯,更新和刪除ASP.NET中的GridView中的行

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

更新 - >

對於刪除 - >>
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方法,你需要重拍,你只是做了...如果你有問題的選擇方法告訴我

相關問題