欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解GridView自帶的編輯刪除更新功能

 更新時間:2016年12月27日 09:35:07   作者:超超boy  
本文主要介紹了GridView自帶的編輯、刪除、更新功能實現(xiàn)的方法。具有一定的參考價值,需要的朋友一起來看下吧

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

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

最新評論