欢迎光临
我们一直在努力

利用DataGrid编辑、修改、删除记录-.NET教程,数据库应用

建站超值云服务器,限时71元/月

if exists (select * from dbo.sysobjects where

id = object_id(n[dbo].[people]) and objectproperty(id, nisusertable) = 1)

drop table [dbo].[people]

go

create table [dbo].[people] (

[pkid] [int] identity (1, 1) not null ,

[firstname] [char] (100) collate chinese_prc_ci_as null ,

[lastname] [char] (100) collate chinese_prc_ci_as null

) on [primary]

go

editdatagridcs.aspx

<%@ page language="c#" debug="true"%>

<%@ import namespace="system.data"%>

<%@ import namespace="system.data.sqlclient" %>

<script runat="server">

//make first sql

string sql = "";

string strcnn = "data source=.;initial catalog=aa;user id=sa;password=;";

//create a datasource function

public icollection createdatasource () {

sqlconnection conn = new sqlconnection(strcnn);

sqldataadapter db_sqladaptor = new sqldataadapter(sql,conn);

dataset ds = new dataset();

db_sqladaptor.fill(ds,"mydataresult");

dataview myview = ds.tables["mydataresult"].defaultview;

return myview;

}

//do page load

public void page_load(object sender, eventargs e) {

strcnn = "data source=.;initial catalog=aa;user id=sa;password=;";

if (!ispostback)

{

sql = "select * from people";

people.datasource = createdatasource();

people.databind();

}

}

public void page_grid(object sender, datagridpagechangedeventargs e)

{

sql = "select * from people";

// set currentpageindex to the page the user clicked.

people.currentpageindex = e.newpageindex;

// rebind the data.

people.datasource = createdatasource();

people.databind();

}

public void people_edit(object sender, datagridcommandeventargs e)

{

sql = "select * from people";

people.edititemindex = e.item.itemindex;

people.datasource = createdatasource();

people.databind();

}

public void people_cancel(object sender, datagridcommandeventargs e)

{

sql = "select * from people";

people.edititemindex = -1;

people.datasource = createdatasource();

people.databind();

}

public void people_update(object sender, datagridcommandeventargs e)

{

string firstname = ((textbox)e.item.cells[1].controls[1]).text;

string lastname = ((textbox)e.item.cells[2].controls[1]).text;

sqlconnection connupdate = new sqlconnection(strcnn);

connupdate.open();

string sql_edit = "update people " +

"set firstname = " + firstname.replace("","")+ "," +

"lastname = " + lastname.replace("","")+ "" +

" where pkid = " + e.item.cells[0].text;

sqlcommand sqlcommandupdate = new sqlcommand(sql_edit,connupdate);

sqlcommandupdate.executenonquery();

connupdate.close();

sql = "select * from people";

people.edititemindex = -1;

people.datasource = createdatasource();

people.databind();

}

public void people_delete(object sender, datagridcommandeventargs e) {

sqlconnection conndel = new sqlconnection(strcnn);

conndel.open();

string sql_del = "delete from people " +

" where pkid = " + e.item.cells[0].text;

sqlcommand sqlcommanddel = new sqlcommand(sql_del,conndel);

sqlcommanddel.executenonquery();

conndel.close();

sql = "select * from people";

people.edititemindex = -1;

people.datasource = createdatasource();

people.databind();

}

</script>

<font face="arial" size="3">

<b>edit people</b>

</font>

<br>

<form runat="server">

<asp:datagrid id="people" runat="server"

bordercolor="green"

width="640"

pagesize="5"

allowpaging="true"

onpageindexchanged="page_grid"

borderwidth="1"

cellpadding="3"

autogeneratecolumns="false"

showheader="true"

visible="true"

oneditcommand="people_edit"

oncancelcommand="people_cancel"

onupdatecommand="people_update"

ondeletecommand="people_delete">

<headerstyle bordercolor="white" backcolor="black"

forecolor="white"

font-bold="true"

font-name="arial"

font-size="9" horizontalalign="center"/>

<itemstyle bordercolor=""

backcolor="#fffff0"

forecolor="black"

font-name="arial"

font-size="8"

font-bold="false" horizontalalign="center"/>

<edititemstyle bordercolor=""

backcolor="#fffff0"

forecolor="black"

font-name="arial"

font-size="7"

font-bold="false" horizontalalign="center"/>

<pagerstyle mode="numericpages" font-size="8"/>

<columns>

<asp:boundcolumn headertext="id" readonly="true" datafield="pkid"/>

<asp:templatecolumn>

<headertemplate>

<b> first name </b>

</headertemplate>

<itemtemplate>

<asp:label

text=<%# databinder.eval(container.dataitem, "firstname").tostring().trim() %>

runat="server"/>

</itemtemplate>

<edititemtemplate>

<asp:textbox id="firstname" text=

<%# databinder.eval(container.dataitem, "firstname").tostring().trim() %>

runat="server" width="100%"/>

</edititemtemplate>

</asp:templatecolumn>

<asp:templatecolumn>

<headertemplate>

<b> last name </b>

</headertemplate>

<itemtemplate>

<asp:label

width="200"

text=<%# databinder.eval(container.dataitem, "lastname").tostring().trim() %>

runat="server"/>

</itemtemplate>

<edititemtemplate>

<asp:textbox id="lastname" text=<br>

<%# databinder.eval(container.dataitem, "lastname").tostring().trim() %>

runat="server" width="100%"/>

</edititemtemplate>

</asp:templatecolumn>

<asp:editcommandcolumn

buttontype="linkbutton"

canceltext="cancel"

edittext="edit"

updatetext="update" />

<asp:buttoncolumn text= "delete" commandname="delete"></asp:buttoncolumn>

</columns>

</asp:datagrid>

</form>

本文评论(comments):为了保护您的电子邮件不被骚扰,地址中的个别符号转换成了全角字符!

评论人:凌风雁 电子邮件:yblin@163.com 评论日期:2004年06月12日 03:51:07

我这个没有办法实现删除功能!不知道为什么!!!可不可以帮助解决一下!!

dim rcount,pcount,currentpage as integer

sub page_load(sender as object, e as eventargs) 首次载入

if not page.ispostback then

dim mana as string

mana = request.querystring("mname")

if mana ="" then

response.redirect("/manage/admin.aspx")

exit sub

end if

session("managename") = mana

dim cn as oledbconnection

dim cmd as oledbdataadapter

dim ds as dataset

dim sql as string = "select * from news order by id desc"

dim dsn as string = application("dsn")

cn = new oledbconnection(dsn)

cmd = new oledbdataadapter

cmd.tablemappings.add("table","news")

cmd.selectcommand = new oledbcommand(sql,cn)

ds = new dataset("news")

cmd.fill(ds,"news")

rcount = ds.tables("news").defaultview.count

currentpage = 1

if (rcount mod mylist.pagesize) = 0 then

pcount = rcount/mylist.pagesize

lblrecordcount.text = rcount.tostring()

lblpagecount.text = pcount.tostring()

lblcurrentpage.text= "1"

else

pcount =cint( rcount/mylist.pagesize + 0.5)

lblrecordcount.text = rcount.tostring()

lblpagecount.text = pcount.tostring()

lblcurrentpage.text = "1"

end if

listsize()

mylist.datasource = ds.tables("news").defaultview

mylist.databind()

end if

end sub

sub change_page(sender as object, e as datagridpagechangedeventargs) 换页

mylist.currentpageindex = e.newpageindex

databind()

end sub

sub page_onclick(sender as object,e as commandeventargs) 响应按钮

currentpage = cint(lblcurrentpage.text)

pcount = cint(lblpagecount.text)

dim cmd1 as string

cmd1 = e.commandname

if cmd1 = "next" then

if (currentpage < (pcount)) then

currentpage = currentpage + 1

end if

end if

if cmd1 = "prev" then

if (currentpage > 0) then

currentpage = currentpage – 1

end if

end if

if cmd1 = "first" then

currentpage = 1

end if

if cmd1 = "last" then

currentpage = (pcount)

end if

lblcurrentpage.text = currentpage

listsize()

mylist.currentpageindex = currentpage – 1

databind()

end sub

sub mylist_delete(sender as object, e as datagridcommandeventargs)

dim mycn as oledbconnection

dim mycmd as oledbcommand

dim mysql as string = "delete from news where id = @id"

dim mydsn as string = application("dsn")

mycn = new oledbconnection(mydsn)

mycmd = new oledbcommand(mysql,mycn)

mycmd.parameters.add(new oledbparameter("@id",oledbtype.integer,4))

mycmd.parameters("@id").value =mylist.datakeys(cint(e.item.itemindex).tostring())

mycmd.connection.open()

mycmd.executenonquery()

mycmd.connection.close()

databind()

end sub

function databind()

dim cn as oledbconnection

dim cmd as oledbdataadapter

dim ds as dataset

dim sql as string = "select * from news order by id desc"

dim dsn as string = application("dsn")

cn = new oledbconnection(dsn)

cmd = new oledbdataadapter

cmd.tablemappings.add("table","news")

cmd.selectcommand = new oledbcommand(sql,cn)

ds = new dataset("news")

cmd.fill(ds,"news")

mylist.datasource = ds.tables("news").defaultview

mylist.databind()

end function

function listsize() 判断按钮可用与否

lbnnextpage.enabled = true

lbnprevpage.enabled = true

lbnlastpage.enabled = true

lbnfirstpage.enabled = true

if currentpage = pcount then

lbnnextpage.enabled = false

lbnlastpage.enabled = false

end if

if currentpage = 1 then

lbnprevpage.enabled = false

lbnfirstpage.enabled = false

end if

if currentpage = 1 and pcount = 1 then

lbnnextpage.enabled = false

lbnprevpage.enabled = false

lbnlastpage.enabled = false

lbnfirstpage.enabled = false

end if

end function

function formatstring(str as string) as string 格式化输出字符

str=str.replace(" "," ")

str=str.replace("<","<")

str=str.replace(">",">")

str=str.replace(vbcrlf,"

")

formatstring = str

end function

function getchar(str_biaoti as string) as string 截取字符

if (str_biaoti.length>20)

return str_biaoti.substring(0,20)+"……"

else

return str_biaoti

end if

end function

function newwin(url)

{

var popup;

url=url;

popup=window.open(url,null,"top=5,left=5,width=600,resizable=no,height=500,menubar=no,toolbar=no,scrollbars=yes,status=no");

popup.focus();

}

新闻管理系统

allowpaging="true"

pagerstyle-visible="false"

pagesize="10"

onpageindexchanged="change_page"

runat="server"

width="550"

gridlines="none"

showfooter="false"

cellpadding="2"

cellspacing="1"

font-name="宋体"

font-size="8pt"

enableviewstate="false"

autogeneratecolumns="false"

datakeyfield="id"

ondeletecommand="mylist_delete">

headertext=""

datanavigateurlfield="id"

datanavigateurlformatstring="newsedit.aspx?id={0}"

datatextfield="标题"

>

共有条记录

当前为/页

评论人:lqidiot 电子邮件:lq9904@tom.com 评论日期:2004年04月12日 03:37:10

有没有用vb.net语言写的,

我写了一个,但是不能实现"更新"这一功能

程序如下,能不能帮我改正一下

多谢了

dim conn as new sqlclient.sqlconnection("server=(local); database=lq; user id=sa; password=506")

conn.open()

dim cmd as sqlclient.sqlcommand

dim sql as string

sql = "update zhuce set [学号]=@学号, [姓名]=@姓名, [性别]=@性别, [专业]=@专业 where [学号]=@key"

cmd = new sqlclient.sqlcommand(sql, conn)

cmd.parameters.add(new sqlclient.sqlparameter("@学号", sqldbtype.int))

cmd.parameters.add(new sqlclient.sqlparameter("@姓名", sqldbtype.char, 10))

cmd.parameters.add(new sqlclient.sqlparameter("@性别", sqldbtype.char, 2))

cmd.parameters.add(new sqlclient.sqlparameter("@专业", sqldbtype.char, 25))

cmd.parameters.add(new sqlclient.sqlparameter("@key", sqldbtype.int))

dim tb(4) as textbox

tb(1) = e.item.cells(2).controls(0) 学号 textbox

tb(2) = e.item.cells(3).controls(0) 姓名 textbox

tb(3) = e.item.cells(4).controls(0) 性别 textbox

tb(4) = e.item.cells(5).controls(0) 专业 textbox

label1.text = val(tb(1).text) & tb(2).text & tb(3).text & tb(4).text

cmd.parameters("@key").value = mygrid.datakeys(e.item.itemindex)

cmd.parameters("@学号").value = val(tb(1).text)

cmd.parameters("@姓名").value = tb(2).text

cmd.parameters("@性别").value = tb(3).text

cmd.parameters("@专业").value = tb(4).text

cmd.executenonquery()

label2.text = cmd.parameters("@学号").value & cmd.parameters("@姓名").value & cmd.parameters("@性别").value & cmd.parameters("@专业").value

conn.close()

mygrid.edititemindex = -1

open_bind()

评论人:aloner007 电子邮件:aloner_007@163.com 评论日期:2004年03月26日 08:47:47

string strcnn = "data source=.;initial catalog=aa;user id=sa;password=;"; //这句语句是什么意思?

db_sqladaptor.fill(ds,"mydataresult");中的mydataresult参数是什么意思!

评论人:http://dotnet.aspx.cc 电子邮件:http://dotnet.aspx.cc 评论日期:2004年02月05日 12:36:23

http://dotnet.aspx.cc

作者blog:http://blog.csdn.net/abaowu/

相关文章

mysql 忘记口令的解决办法

mysql的应用尝试过5千万条记录

eweek 权威测试显示mysql 4.0.1 可与oracle 9i 媲美

microsoft access 数据库常规规格

asp.net 配置文件的格式

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用DataGrid编辑、修改、删除记录-.NET教程,数据库应用
分享到: 更多 (0)