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语句呢! 展开
<%@ 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语句呢! 展开
2个回答
展开全部
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();
}
}
更新之后,重新数据绑定下。
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();
}
}
更新之后,重新数据绑定下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |