熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

GridView控件實現數據項的編輯、更新、取消

2013-11-13 09:48:51  來源: .NET編程 

   using System;

   using SystemData;

   using SystemDataSqlClient;

   using SystemConfiguration;

   using SystemCollections;

   using SystemWeb;

   using SystemWebSecurity;

   using SystemWebUI;

   using SystemWebUIWebControls;

   using SystemWebUIWebControlsWebParts;

   using SystemWebUIHtmlControls;

  

   public partial class Demo : SystemWebUIPage

   {

       protected void Page_Load(object sender EventArgs e)

       {

           if (PageIsPostBack == false)

           {

               BindData();

           }

       }

  

       public void BindData()

       {

           string strSql = select UserIDC_NameE_NameQQ from Demo_User ;

           DataTable dt = SqlHelperExecuteDataset(SqlHelperCONN_STRING CommandTypeText strSql null)Tables[];

  

           GridViewDataSource = dt;

           GridViewDataKeyNames = new string[] { UserID };//主鍵

           GridViewDataBind();

       }

  

       protected void GridView_PageIndexChanging(object sender GridViewPageEventArgs e)

       {

           GridViewPageIndex = eNewPageIndex;

           BindData();

       }

  

       protected void GridView_RowEditing(object sender GridViewEditEventArgs e)

       {

           GridViewEditIndex = eNewEditIndex;

           BindData();

       }

  

       protected void GridView_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)

       {

           GridViewEditIndex = ;

           BindData();

       }

  

       protected void GridView_RowUpdating(object sender GridViewUpdateEventArgs e)

       {

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

           SqlParameter[] para = {

                                   new SqlParameter(@QQ((TextBox)(GridViewRows[eRowIndex]Cells[]Controls[]))TextToString()Trim())

                                   new SqlParameter(@UserID(int)GridViewDataKeys[eRowIndex]Value)

                                  };

           SqlHelperExecuteNonQuery(SqlHelperCONN_STRING CommandTypeText strSql para);

  

           GridViewEditIndex = ;

           BindData();

       }

   }

  

  

  

   <table align=center bgcolor=#cde border= cellpadding= cellspacing= width=%>

           <tr>

               <th colspan=>

                   GridView演示</th>

           </tr>

          <tr>

              <td colspan= >

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

                 OnRowCancelingEdit=GridView_RowCancelingEdit OnRowEditing=GridView_RowEditing OnRowUpdating=GridView_RowUpdating  >

                   <Columns>

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

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

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

                         <asp:BoundField DataField=QQ HeaderText=QQ帳號 />

                       <asp:CommandField HeaderText=編輯 ShowEditButton=True />

                     </Columns>

                     <RowStyle HorizontalAlign=Center />

                     <PagerStyle HorizontalAlign=Right />

                 </asp:GridView>

              </td>

          </tr>

  

  

   </table>

  


From:http://tw.wingwit.com/Article/program/net/201311/11647.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.