java datagrid控件_使用DataGrid控件更新删除数据示例

/*UpdateDelForm.aspx

前台程序代码

*/

AutoEventWireup="false" Inherits="UpdateDelDbDemo.UpdateDelForm" %>

WebForm1

ButtonType="LinkButton" UpdateText="更新" CancelText="取消"

EditText="编辑">

/*UpdateDelForm.aspx.cs

后台程序代码

*/

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace UpdateDelDbDemo

{

///

/// WebForm1 的摘要说明。

///

public class UpdateDelForm : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid dgDemo;

private string strCon="server=JOSEN;database=testDb;integrated security=true";

private string strSqlSelect="select * from UserInfo";

private SqlConnection objCon;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

if(!IsPostBack)

LoadGrid();

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.dgDemo.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_CancelCommand);

this.dgDemo.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_EditCommand);

this.dgDemo.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_UpdateCommand);

this.dgDemo.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDemo_DeleteCommand);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

//读取数据并帮定到DataGrid

private void LoadGrid()

{

Connect();

SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);

DataSet ds=new DataSet();

objAdpt.Fill(ds,"dtUserInfo");

dgDemo.DataSource=ds;

dgDemo.DataBind();

}

//建立数据库的连接

private void Connect()

{

if(objCon==null)

objCon=new SqlConnection(strCon);

if(objCon.State==ConnectionState.Closed)

objCon.Open();

}

//断开数据库的连接

private void Disconnect()

{

objCon.Close();

}

private void dgDemo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

dgDemo.EditItemIndex=e.Item.ItemIndex;

LoadGrid();

}

private void dgDemo_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

dgDemo.EditItemIndex=-1;

LoadGrid();

}

private void dgDemo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string strUserID=e.Item.Cells[0].Text;

//string strUserID=((TextBox)e.Item.Cells[0].Controls[0]).Text;

string strFirstName=((TextBox)e.Item.Cells[1].Controls[0]).Text;

string strLastName=((TextBox)e.Item.Cells[2].Controls[0]).Text;

string strAddress=((TextBox)e.Item.Cells[3].Controls[0]).Text;

dgDemo.EditItemIndex=-1;

//此方法具体处理数据库的更新

UpdateRecord(strUserID,strFirstName,strLastName,strAddress);

//调用此方法从数据库读取数据

LoadGrid();

}

private void UpdateRecord(string strUserID, string strFirstName, string strLastName, string strAddress)

{

Connect();

SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);

DataSet ds=new DataSet();

objAdpt.Fill(ds,"dtUserInfo");

Disconnect();

DataTable tbl=ds.Tables["dtUserInfo"];

//设定表的主键

DataColumn[] myKey=new DataColumn[1];

myKey[0]=tbl.Columns["ID"];

tbl.PrimaryKey=myKey;

DataRow dr=tbl.Rows.Find(strUserID);

//dr["ID"]=strUserID;

dr["FirstName"]=strFirstName;

dr["LastName"]=strLastName;

dr["address"]=strAddress;

SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);

Connect();

objAdpt.Update(ds,"dtUserInfo");

Disconnect();

}

private void dgDemo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string strUserID=e.Item.Cells[0].Text;

dgDemo.EditItemIndex=-1;

DelRecord(strUserID);

//调用此方法从数据库读取数据

LoadGrid();

}

private void DelRecord(string strUserID)

{

Connect();

SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlSelect,objCon);

DataSet ds=new DataSet();

objAdpt.Fill(ds,"dtUserInfo");

Disconnect();

DataTable tbl=ds.Tables["dtUserInfo"];

//设定表的主键

DataColumn[] myKey=new DataColumn[1];

myKey[0]=tbl.Columns["ID"];

tbl.PrimaryKey=myKey;

DataRow dr=tbl.Rows.Find(strUserID);

dr.Delete();

SqlCommandBuilder cb=new SqlCommandBuilder(objAdpt);

Connect();

objAdpt.Update(ds,"dtUserInfo");

Disconnect();

}

}

}

5d722fe088c0b869117f2df143af5337.png

苦笑枯 2007-01-19 00:15 发表评论

文章来源:http://www.blogjava.net/kuxiaoku/articles/94807.html

posted on 2007-01-19 00:15 苦笑枯 阅读(310) 评论(0)  编辑  收藏 所属分类: C#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值