利用DataTable进行删除GridView中的数据方法之新思路
2013-10-23 11:07
344 查看
public partial class DataSet_Delete : System.Web.UI.Page
{
private string strConn = "data source=localhost;initial catalog=Northwind;user id=sa;password=sa";
SqlConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用LoadGridView方法以填充和显示数据
LoadGridView();
}
}
private void ConnectionDB()
{
if (Conn == null)
{
//如果SqlConnection对象不存在新建该对象
Conn = new SqlConnection(strConn);
}
if (Conn.State == ConnectionState.Closed)
{
//如果Connection对象的State状态为关闭Closed就将其打开
Conn.Open();
}
}
//创建CloseConnection方法,实现关闭数据库的操作
private void CloseConnection()
{
Conn.Close();
}
//创建LoadGridView方法,实现填充GridView控件以显示数据
private void LoadGridView()
{
string SelectSql = "SELECT * FROM Categories";
ConnectionDB();
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SelectSql, Conn);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "Categories");
CloseConnection();
GVCategory.DataSource = myDataSet.Tables["Categories"];
GVCategory.DataBind();
}
protected void RowDelete(object sender, GridViewDeleteEventArgs e)
{
int CategroyID = Convert.ToInt32(GVCategory.Rows[e.RowIndex].Cells[0].Text);
GVCategory.EditIndex = -1;
ConnectionDB();
string SelectSql = "SELECT * FROM Categories";
SqlDataAdapter myDA = new SqlDataAdapter(SelectSql, Conn);
DataSet DS = new DataSet();
myDA.Fill(DS, "Categories");
CloseConnection();
//创建本地数据表table
DataTable table = DS.Tables["Categories"];
//设置table的PrimaryKey属性,其主键码设置为CategoryID列
table.PrimaryKey = new DataColumn[] { table.Columns["CategoryID"] };
//使用Find方法查找当前进行删除的数据行
DataRow delRow = table.Rows.Find(CategroyID);
//使用Delete方法删除数据行
delRow.Delete();
//创建SqlCommandBuilder对象myCB,它将自动创建DELETE语句
SqlCommandBuilder myCB = new SqlCommandBuilder(myDA);
ConnectionDB();
//使用DataAdapter对象的Update方法将删除数据行后的table表推入到数据库中
myDA.Update(table);
CloseConnection();
//重新载入数据,显示删除数据后的数据表内容
LoadGridView();
}
}
{
private string strConn = "data source=localhost;initial catalog=Northwind;user id=sa;password=sa";
SqlConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用LoadGridView方法以填充和显示数据
LoadGridView();
}
}
private void ConnectionDB()
{
if (Conn == null)
{
//如果SqlConnection对象不存在新建该对象
Conn = new SqlConnection(strConn);
}
if (Conn.State == ConnectionState.Closed)
{
//如果Connection对象的State状态为关闭Closed就将其打开
Conn.Open();
}
}
//创建CloseConnection方法,实现关闭数据库的操作
private void CloseConnection()
{
Conn.Close();
}
//创建LoadGridView方法,实现填充GridView控件以显示数据
private void LoadGridView()
{
string SelectSql = "SELECT * FROM Categories";
ConnectionDB();
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SelectSql, Conn);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "Categories");
CloseConnection();
GVCategory.DataSource = myDataSet.Tables["Categories"];
GVCategory.DataBind();
}
protected void RowDelete(object sender, GridViewDeleteEventArgs e)
{
int CategroyID = Convert.ToInt32(GVCategory.Rows[e.RowIndex].Cells[0].Text);
GVCategory.EditIndex = -1;
ConnectionDB();
string SelectSql = "SELECT * FROM Categories";
SqlDataAdapter myDA = new SqlDataAdapter(SelectSql, Conn);
DataSet DS = new DataSet();
myDA.Fill(DS, "Categories");
CloseConnection();
//创建本地数据表table
DataTable table = DS.Tables["Categories"];
//设置table的PrimaryKey属性,其主键码设置为CategoryID列
table.PrimaryKey = new DataColumn[] { table.Columns["CategoryID"] };
//使用Find方法查找当前进行删除的数据行
DataRow delRow = table.Rows.Find(CategroyID);
//使用Delete方法删除数据行
delRow.Delete();
//创建SqlCommandBuilder对象myCB,它将自动创建DELETE语句
SqlCommandBuilder myCB = new SqlCommandBuilder(myDA);
ConnectionDB();
//使用DataAdapter对象的Update方法将删除数据行后的table表推入到数据库中
myDA.Update(table);
CloseConnection();
//重新载入数据,显示删除数据后的数据表内容
LoadGridView();
}
}
相关文章推荐
- vue遇到过的坑(父组件向输入框组件传值进去,然后子组件利用传进来的数据进行数据绑定的问题与解决方法)
- linux 利用find进行批量删除CVS目录和文件的方法
- ASP.NET中GridView控件删除数据的两种方法
- SQLServer 数据去重的一种处理方法(增加一个自增字段,利用自增,删除重复数据)
- SELECT INTO和INSERT INTO SELECT的区别 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对 C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等) C# MD5 加密,解密 C#中DataTable删除多条数据
- Gridview利用DataFormatString属性设置数据格式的方法
- 在.NET中利用Remoting的数据进行序列化方法
- ASP.NET中GridView控件删除数据的两种方法
- -将DropDownList控件绑定 到GridView控当中,并调用方法给DropDownList控件在页面进行数据绑定
- Java利用String的split方法对实验数据进行简化
- gridview利用 DataView 对象进行排序和过滤数据
- ArrayList利用迭代器Iterator遍历方法实现数据(新闻)的删除、添加、显示功能例子
- 如何利用数据挖掘进行分析的方法
- C#:利用datatable对excel进行数据输入
- 一个使用GridView显示数据,并且可以进行添加、修改、删除操作的例子
- asp.net 站内搜索功能的实现及利用datatable绑定数据并进行分页
- GridView中利用隐藏的TemplateFied来进行数据访问
- Gridview利用DataFormatString属性设置数据格式的方法
- ASP.NET中GridView控件删除数据的两种方法
- 基于演化博弈数据利用压缩感知方法进行网络重构