当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

GridView控件实现数据项的编辑、更新、取消

  1 using System;

  2 using System.Data;

  3 using System.Data.SqlClient;

  4 using System.Configuration;

  5 using System.Collections;

  6 using System.Web;

  7 using System.Web.Security;

  8 using System.Web.UI;

  9 using System.Web.UI.WebControls;

  10 using System.Web.UI.WebControls.WebParts;

  11 using System.Web.UI.HtmlControls;

  12

  13 public partial class Demo8 : System.Web.UI.Page

  14 {

  15     protected void Page_Load(object sender, EventArgs e)

  16     {

  17         if (Page.IsPostBack == false)

  18         {

  19             BindData();

  20         }

  21     }

  22

  23     public void BindData()

  24     {

  25         string strSql = "select UserID,C_Name,E_Name,QQ from Demo_User ";

  26         DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];

  27

  28         GridView.DataSource = dt;

  29         GridView.DataKeyNames = new string[] { "UserID" };//主键

  30         GridView.DataBind();

  31     }

  32

  33     protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)

  34     {

  35         GridView.PageIndex = e.NewPageIndex;

  36         BindData();

  37     }

  38

  39     protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)

  40     {

  41         GridView.EditIndex = e.NewEditIndex;

  42         BindData();

  43     }

  44

  45     protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

  46     {

  47         GridView.EditIndex = -1;

  48         BindData();

  49     }

  50

  51     protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)

  52     {

  53         string strSql = "Update Demo_User set QQ=@QQ where UserID=@UserID ";

  54         SqlParameter[] para = {

  55                                 new SqlParameter("@QQ",((TextBox)(GridView.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()),

  56                                 new SqlParameter("@UserID",(int)GridView.DataKeys[e.RowIndex].Value),

  57                                };

  58         SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql, para);

  59

  60         GridView.EditIndex = -1;

  61         BindData();

  62     }

  63 }

  64

  65

  66

  1 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%">

  2         <tr>

  3             <th colspan="2">

  4                 GridView演示</th>

  5         </tr>

  6        <tr>

  7            <td colspan="2" style="width: 100%;" >

  8               <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12"

  9               OnRowCancelingEdit="GridView_RowCancelingEdit" OnRowEditing="GridView_RowEditing" OnRowUpdating="GridView_RowUpdating"  >

  10                 <Columns>

  11                       <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" />

  12                       <asp:BoundField DataField="C_Name" HeaderText="中文名字" ReadOnly="True" />

  13                       <asp:BoundField DataField="E_Name" HeaderText="英文名字" ReadOnly="True" />

  14                       <asp:BoundField DataField="QQ" HeaderText="QQ帐号" />

  15                     <asp:CommandField HeaderText="编辑" ShowEditButton="True" />

  16                   </Columns>

  17                   <RowStyle HorizontalAlign="Center" />

  18                   <PagerStyle HorizontalAlign="Right" />

  19               </asp:GridView>

  20            </td>

  21        </tr>

  22

  23

  24 </table>

相关内容
赞助商链接