接触ASP.NET一年了,虽然做了很多项目,但是很少去总结,今天看到几个朋友还在为GridView用SqlDataAdapter做连接类读取,修改,删除而发愁,我决定总结一下,把常出错的地方和用法告诉大家,不全的请留言补充,为了像我一样的初学者...好了,有不懂得留言问我,看过要留言啊...呵呵
直接看CS代码吧,aspx代码就是一个GridView表和定义的删除,修改事件:
代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
// 使用静态类是为了删除,修改方便
static DataSet set = new DataSet(); // 存放数据用
static SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(); // 生成连接字符串
static SqlDataAdapter adapter = null ; // 连接类
protected void Page_Load( object sender, EventArgs e)
{
// 这里要避免回发重复绑定,如果不做挥发判定,就不能获取编辑状态修改后的新值,这个要注意
if ( ! this .Page.IsPostBack)
{
bind(); // 数据绑定
}
}
private void bind()
{
conn.DataSource = @" SWK-PC/SQLSERVER " ;
conn.InitialCatalog = " jyoa " ;
conn.UserID = " sa " ;
conn.Password = " 19871123 " ;
adapter = new SqlDataAdapter( " select * from Models " , conn.ConnectionString);
SqlCommandBuilder cb = new SqlCommandBuilder(adapter); // 这个是必要的,他会自动生成删除和更新语句
adapter.Fill( set , " swk " );
GridView1.DataSource = set .Tables[ " swk " ];
GridView1.DataBind();
}
// 删除
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
{
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex].Delete();
adapter.Update( set , " swk " );
bind();
}
// 编辑
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
// 更新
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
// 这里的母的主要是循环读取每个表格的数据,不管更改与否
for ( int i = 2 ; i < GridView1.Rows[e.RowIndex].Cells.Count; i ++ )
{
// 这里判断自动生成的字段是不是CheckBox类型,因为如果数据库里是bool类型就会自动生成CheckBox
if (GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ] is CheckBox)
{
if (((CheckBox)(GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ])).Checked == true )
{
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = true ;
}
else
{ // Rose[]里面是在Tables["swk"]表里的索引,当前页数乘以每页的个数再加上当前索引就是在Tables里的索引
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = false ;
}
}
else
{
string sss = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ])).Text;
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = sss ;
}
}
adapter.Update( set , " swk " );
GridView1.EditIndex = - 1 ;
bind();
}
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = - 1 ;
bind();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
// 使用静态类是为了删除,修改方便
static DataSet set = new DataSet(); // 存放数据用
static SqlConnectionStringBuilder conn = new SqlConnectionStringBuilder(); // 生成连接字符串
static SqlDataAdapter adapter = null ; // 连接类
protected void Page_Load( object sender, EventArgs e)
{
// 这里要避免回发重复绑定,如果不做挥发判定,就不能获取编辑状态修改后的新值,这个要注意
if ( ! this .Page.IsPostBack)
{
bind(); // 数据绑定
}
}
private void bind()
{
conn.DataSource = @" SWK-PC/SQLSERVER " ;
conn.InitialCatalog = " jyoa " ;
conn.UserID = " sa " ;
conn.Password = " 19871123 " ;
adapter = new SqlDataAdapter( " select * from Models " , conn.ConnectionString);
SqlCommandBuilder cb = new SqlCommandBuilder(adapter); // 这个是必要的,他会自动生成删除和更新语句
adapter.Fill( set , " swk " );
GridView1.DataSource = set .Tables[ " swk " ];
GridView1.DataBind();
}
// 删除
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
{
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex].Delete();
adapter.Update( set , " swk " );
bind();
}
// 编辑
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
// 更新
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
// 这里的母的主要是循环读取每个表格的数据,不管更改与否
for ( int i = 2 ; i < GridView1.Rows[e.RowIndex].Cells.Count; i ++ )
{
// 这里判断自动生成的字段是不是CheckBox类型,因为如果数据库里是bool类型就会自动生成CheckBox
if (GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ] is CheckBox)
{
if (((CheckBox)(GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ])).Checked == true )
{
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = true ;
}
else
{ // Rose[]里面是在Tables["swk"]表里的索引,当前页数乘以每页的个数再加上当前索引就是在Tables里的索引
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = false ;
}
}
else
{
string sss = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[i].Controls[ 0 ])).Text;
set .Tables[ " swk " ].Rows[GridView1.PageIndex * GridView1.PageSize + e.RowIndex][i - 1 ] = sss ;
}
}
adapter.Update( set , " swk " );
GridView1.EditIndex = - 1 ;
bind();
}
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = - 1 ;
bind();
}
}
}