主頁 > 知識庫 > asp.net中使用DatagridView的增刪改方法具體實現(xiàn)

asp.net中使用DatagridView的增刪改方法具體實現(xiàn)

熱門標(biāo)簽:智能電話營銷外呼系統(tǒng) 戶外地圖標(biāo)注軟件手機(jī)哪個好用 凱立德導(dǎo)航官網(wǎng)地圖標(biāo)注 五常地圖標(biāo)注 長春呼叫中心外呼系統(tǒng)哪家好 鄭州400電話辦理 聯(lián)通 萊蕪?fù)夂綦婁N機(jī)器人價格 電銷語音自動機(jī)器人 地圖標(biāo)注和認(rèn)領(lǐng)
default.aspx 頁面:
復(fù)制代碼 代碼如下:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GPS_Web.Default" %>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head runat="server">
    title>/title>
/head>
body>
    form id="form1" runat="server">
    div>
        !--GridView中必須寫的幾個事件:onrowediting、onrowupdating、onrowcancelingedit、onrowdeleting--->
        asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
            onrowdeleting="GridView1_RowDeleting"
            onselectedindexchanging="GridView1_SelectedIndexChanging">
            Columns>
                asp:TemplateField HeaderText="編號" Visible="False">
                    ItemTemplate>
                        asp:Label ID="lblNum" runat="server" Text='%# Eval("Num") %>'>/asp:Label>
                    /ItemTemplate>
                /asp:TemplateField>
                asp:TemplateField HeaderText="姓名">
                    EditItemTemplate>
                        asp:TextBox ID="txtTableName" runat="server" Text='%# Eval("TableName") %>'>/asp:TextBox>
                    /EditItemTemplate>
                    ItemTemplate>
                        asp:Label ID="Label2" runat="server" Text='%# Eval("TableName") %>'>/asp:Label>
                    /ItemTemplate>
                /asp:TemplateField>
                asp:TemplateField HeaderText="地址">
                    EditItemTemplate>
                        asp:TextBox ID="txtTextName" runat="server" Text='%# Eval("TextName") %>'>/asp:TextBox>
                    /EditItemTemplate>
                    ItemTemplate>
                        asp:Label ID="Label3" runat="server" Text='%# Eval("TextName") %>'>/asp:Label>
                    /ItemTemplate>
                /asp:TemplateField>
                asp:CommandField HeaderText="狀態(tài)" ShowSelectButton="True" />
                asp:CommandField HeaderText="編輯" ShowEditButton="True" />
                asp:CommandField HeaderText="刪除" ShowDeleteButton="True" />
            /Columns>
        /asp:GridView>

        br />
nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;
        asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="添加" />
        br />
        br />   
    /div>
    /form>
/body>
/html>

default.aspx.cs頁面代碼:
復(fù)制代碼 代碼如下:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPS_Web.ywpages.DAL;
using System.Data;
namespace GPS_Web
{
    /// summary>
    /// 參考的網(wǎng)址:http://blog.csdn.net/wanglei_samrtfish/article/details/8070480
    /// /summary>
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridViewBind();
            }
        }
        private void GridViewBind()
        {
            string sql = "select Num,TableName,TextName from dbo.GroupType_Demo";
            try
            {     //綁定數(shù)據(jù)源
                GridView1.DataSource = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0];
                GridView1.DataBind();
            }
            catch (Exception ex){}
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridViewBind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {  //gridview編輯項索引等于單擊行的索引
            GridView1.EditIndex = e.NewEditIndex;
            GridViewBind();
        }
        /// summary>
        /// 修改事件
        /// /summary>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {  //取出編號
            int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
            //獲取修改的值的內(nèi)容
            string TableName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTableName")).Text;
            string TextName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTextName")).Text;
            //更新記錄
            string sql = string.Format("update dbo.GroupType_Demo set TableName='{0}',TextName='{1}' where Num={2}",TableName,TextName,Num);
            try
            {
                int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('修改成功!')/script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('修改失敗!')/script>");
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('操作無效!')/script>");
            }
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        /// summary>
        /// 刪除事件
        /// /summary>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //取出編號
            int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
            //更新記錄
            string sql = string.Format("delete dbo.GroupType_Demo where num={0}", Num);
            try
            {
                int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
                if (i > 0)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('刪除成功!')/script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('刪除失??!')/script>");
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "script language='javascript'>alert('操作無效!')/script>");
            }  www.jb51.net
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        /// summary>
        /// 添加按鈕
        /// /summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default_Add.aspx");
        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //行號
            int i = e.NewSelectedIndex;
            GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

            btnAdd.Text = "你選中了第" + (i+1) + "行。";
        }       
    }
}

執(zhí)行后的頁面效果:
您可能感興趣的文章:
  • ASP.NET2.0中用Gridview控件操作數(shù)據(jù)的代碼
  • asp.net中GridView和DataGrid相同列合并實現(xiàn)代碼
  • ASP.NET GridView 實現(xiàn)課程表顯示(動態(tài)合并單元格)實現(xiàn)步驟
  • ASP.NET GridView中文本內(nèi)容無法換行(自動換行/正常換行)
  • ASP.NET生成樹形顯示的GridView實現(xiàn)思路
  • asp.net的GridView控件使用方法大全
  • Asp.net GridView使用大全(分頁實現(xiàn))
  • ASP.NET中的DataGridView綁定數(shù)據(jù)和選中行刪除功能具體實例
  • asp.net讀取excel中的數(shù)據(jù)并綁定在gridview
  • asp.net中的GridView分頁問題
  • asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
  • ASP.NET中為GridView添加刪除提示框的方法
  • ASP.NET中日歷控件和JS版日歷控件的使用方法(第5節(jié))
  • asp.net新聞列表生成靜態(tài)頁之批量和單頁生成
  • ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布(1)
  • 簡析ASP.NET網(wǎng)站的創(chuàng)建與發(fā)布過程
  • ASP.NET MVC3模板頁的使用(2)
  • ASP.NET MVC4之js css文件合并功能(3)
  • 創(chuàng)建第一個ASP.NET應(yīng)用程序(第1節(jié))
  • ASP.NET網(wǎng)站模板的實現(xiàn)(第2節(jié))
  • ASP.NET網(wǎng)站聊天室的設(shè)計與實現(xiàn)(第3節(jié))
  • ASP.NET實現(xiàn)用戶注冊和驗證功能(第4節(jié))
  • ASP.NET在線文本編輯控件的使用(第6節(jié))
  • GridView控件實現(xiàn)數(shù)據(jù)的修改(第9節(jié))

標(biāo)簽:宣城 紅河 岳陽 福州 西寧 西藏 衢州 湖州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net中使用DatagridView的增刪改方法具體實現(xiàn)》,本文關(guān)鍵詞  asp.net,中,使用,DatagridView,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net中使用DatagridView的增刪改方法具體實現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net中使用DatagridView的增刪改方法具體實現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章