.net中使用DatagridView的增删改方法
来源: 阅读:678 次 日期:2014-08-29 10:48:57
温馨提示: 小编为您整理了“.net中使用DatagridView的增删改方法”,方便广大网友查阅!

default.aspx 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GPS_Web.Default" %>

onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit"

onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"

onrowdeleting="GridView1_RowDeleting"

onselectedindexchanging="GridView1_SelectedIndexChanging">




Baidu
map

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

{

///

///

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

}

///

/// 修改事件

///

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", "");

}

else

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");

}

}

catch (Exception ex)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");

}

GridView1.EditIndex = -1;

GridViewBind();

}

///

/// 删除事件

///

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", "");

}

else

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");

}

}

catch (Exception ex)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");

}

GridView1.EditIndex = -1;

GridViewBind();

}

///

/// 添加按钮

///

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) + "行。";

}

}

}

执行后的页面效果:

更多信息请查看IT技术专栏

更多信息请查看 网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 加入群交流| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map