最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

asp.net中使用DatagridView的增删改方法具体实现

来源:懂视网 责编:小采 时间:2020-11-27 22:40:59
文档

asp.net中使用DatagridView的增删改方法具体实现

asp.net中使用DatagridView的增删改方法具体实现:default.aspx 页面: 代码如下:<%@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=GPS_Web.Default %><!DOCTYPE html PUBLIC -//W3C//DTD XHTML
推荐度:
导读asp.net中使用DatagridView的增删改方法具体实现:default.aspx 页面: 代码如下:<%@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=GPS_Web.Default %><!DOCTYPE html PUBLIC -//W3C//DTD XHTML

default.aspx 页面:
代码如下:
<%@ 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="状态" ShowSelectButton="True" />
                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>

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

default.aspx.cs页面代码:
代码如下:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPS_Web.ywpages.DAL;
using System.Data;
namespace GPS_Web
{
    /// <summary>
    /// 参考的网址: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
            {     //绑定数据源
                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());
            //获取修改的值的内容
            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.gxlcms.com
            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) + "行。";
        }       
    }
}

执行后的页面效果:

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

asp.net中使用DatagridView的增删改方法具体实现

asp.net中使用DatagridView的增删改方法具体实现:default.aspx 页面: 代码如下:<%@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=GPS_Web.Default %><!DOCTYPE html PUBLIC -//W3C//DTD XHTML
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top