您好,登錄后才能下訂單哦!
這篇文章主要介紹GridView如何使用自帶編輯刪除更新功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
效果圖:
前臺代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridView_bianjidelete.aspx.cs" Inherits="gridView_bianjidelete" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="ID" HeaderText="產品ID" ReadOnly="True" /> <asp:BoundField DataField="name" HeaderText="產品name" /> <asp:BoundField DataField="stock" HeaderText="庫存" /> <asp:CommandField HeaderText="選擇" ShowSelectButton="True" /> <asp:CommandField HeaderText="編輯" ShowEditButton="True" /> <asp:CommandField HeaderText="刪除" ShowDeleteButton="True" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="Red" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> </div> </form> </body> </html>
后臺代碼:
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class gridView_bianjidelete : System.Web.UI.Page {//清清月兒http://blog.csdn.net/21aspnet SqlConnection sqlcon; SqlCommand sqlcom; string strCon = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } //刪除之后重新綁定 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from product where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.DataBind(); bind(); } //更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlcon = new SqlConnection(strCon); string sqlstr = "update product set name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',stock='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcom = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; // GridView1.DataBind(); bind(); } //取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } //綁定 public void bind() { string sqlstr = "select * from product p,Uuser u where p.userid=u.id"; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "datatable"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "id" };//主鍵 GridView1.DataBind(); sqlcon.Close(); } }
以上是“GridView如何使用自帶編輯刪除更新功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。