SqlDataAdapter操作Gridview实现Gridview编辑功能

ASP.NET里用GridView控件显示数据后,请问如何对GridView里显示的数据进行编辑,增加,删除,同时同步到数据库里。这是我的源代码,请高手指教下,最好是能给... ASP.NET里用GridView控件显示数据后,请问如何对GridView里显示的数据进行编辑,增加,删除,同时同步到数据库里。这是我的源代码,请高手指教下,最好是能给个全例子,谢谢!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
<asp:GridView ID="GridView1" runat="server" Style="position: relative" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DsTest();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string con_Str = "select * from Book";
SqlConnection mycon = new SqlConnection();
mycon.connection="private static string sConn = "Data Source=MOTO_SERVER;Initial Catalog=ZYDB;User ID=sa;Password=moto";
SqlCommand cmd = new SqlCommand(con_Str,mycon.conn);
da.SelectCommand = cmd;
da.Fill(ds, "Book");
GridView1.DataSource = ds.Tables["Book"].DefaultView;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;

}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{

}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
da.Update(ds, "Book");

}
}
谢谢各位,想请教下12666954 那个SqlCommand cmd=new SqlCommand("update语句",myconn);
cmd.ExecuteNonQuery();
databind();
中的update语句怎么来哦,怎么从gridview中编辑字段并且读取到update语句呢!
展开
 我来答
12666954
2007-09-27 · TA获得超过386个赞
知道小有建树答主
回答量:381
采纳率:0%
帮助的人:337万
展开全部
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DsTest();
SqlConnection mycon = new SqlConnection();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
databind();

}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
SqlCommand cmd=new SqlCommand("update语句",myconn);
cmd.ExecuteNonQuery();
databind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
da.Update(ds, "Book");

}
public void databind()
{
string con_Str = "select * from Book";
mycon.connection="private static string sConn = "Data Source=MOTO_SERVER;Initial Catalog=ZYDB;User ID=sa;Password=moto";
SqlCommand cmd = new SqlCommand(con_Str,mycon.conn);
da.SelectCommand = cmd;
da.Fill(ds, "Book");
GridView1.DataSource = ds.Tables["Book"].DefaultView;
GridView1.DataBind();
}
}
更新之后,重新数据绑定下。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友eb0081a7e
2007-09-27 · TA获得超过1081个赞
知道小有建树答主
回答量:788
采纳率:0%
帮助的人:0
展开全部
邮箱告我传个简单的增删改查分页的DataList用法,
GridView类似这么做.
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式