RowUpdated和RowUpdating
当使用 Update 时,每一个更新的数据行都会发生两个事件。执行顺序如下:
将 DataRow 中的值移至参数值。
引发 OnRowUpdating 事件。
执行命令。
如果该命令设置为 FirstReturnedRecord,返回的第一项结果将放置在 DataRow 中。
如果存在输出参数,它们将被放在 DataRow 中。
引发 OnRowUpdated 事件。
调用 AcceptChanges。
下面的示例演示正在使用的 RowUpdating 和 RowUpdated 事件。
public static void CreateDataAdapter(
string connectionString)
...{
using (OleDbConnection connection = new OleDbConnection(connectionString))
...{
OleDbDataAdapter adapter = new OleDbDataAdapter(
"SELECT * FROM Customers WHERE CustomerID = ''ALFKI''", connection);
adapter.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)",
connection);
adapter.InsertCommand.Parameters.Add(
"@CustomerID", OleDbType.VarChar, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName", OleDbType.VarChar, 30, "CompanyName");
connection.Open();
DataSet custDS = new DataSet();
adapter.Fill(custDS, "Customers");
DataRow custRow = custDS.Tables["Customers"].NewRow();
custRow["CustomerID"] = "NEWCO";
custRow["CompanyName"] = "New Company";
custDS.Tables["Customers"].Rows.Add(custRow);
// add handlers
adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);
adapter.Update(custDS, "Customers");
// remove handlers
adapter.RowUpdating -= new OleDbRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated -= new OleDbRowUpdatedEventHandler(OnRowUpdated);
foreach (DataRow row in custDS.Tables["Customers"].Rows)
...{
if (row.HasErrors)
Console.WriteLine(row.RowError);
}
}
}
protected static void OnRowUpdating(object sender,
OleDbRowUpdatingEventArgs args)
...{
if (args.StatementType == StatementType.Insert)
...{
System.IO.TextWriter writer = System.IO.File.AppendText("Inserts.log");
writer.WriteLine("{0}: Customer {1} Inserted.",
DateTime.Now, args.Row["CustomerID"]);
writer.Close();
}
}
protected static void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
...{
if (args.Status == UpdateStatus.ErrorsOccurred)
...{
args.Row.RowError = args.Errors.Message;
args.Status = UpdateStatus.SkipCurrentRow;
}
}
using System;
using System.Data;
using System.Data.OleDb;
class Class1
...{
static void Main()
...{
string x = "Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind";
CreateDataAdapter(x);
}
public static void CreateDataAdapter(
string connectionString)
...{
using (OleDbConnection connection = new OleDbConnection(connectionString))
...{
OleDbDataAdapter adapter = new OleDbDataAdapter(
"SELECT * FROM Customers WHERE CustomerID = ''ALFKI''", connection);
adapter.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)",
connection);
adapter.InsertCommand.Parameters.Add(
"@CustomerID", OleDbType.VarChar, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add(
"@CompanyName", OleDbType.VarChar, 30, "CompanyName");
connection.Open();
DataSet custDS = new DataSet();
adapter.Fill(custDS, "Customers");
DataRow custRow = custDS.Tables["Customers"].NewRow();
custRow["CustomerID"] = "NEWCO";
custRow["CompanyName"] = "New Company";
custDS.Tables["Customers"].Rows.Add(custRow);
// add handlers
adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);
adapter.Update(custDS, "Customers");
// remove handlers
adapter.RowUpdating -= new OleDbRowUpdatingEventHandler(OnRowUpdating);
adapter.RowUpdated -= new OleDbRowUpdatedEventHandler(OnRowUpdated);
foreach (DataRow row in custDS.Tables["Customers"].Rows)
...{
if (row.HasErrors)
Console.WriteLine(row.RowError);
}
}
}
protected static void OnRowUpdating(object sender,
OleDbRowUpdatingEventArgs args)
...{
if (args.StatementType == StatementType.Insert)
...{
System.IO.TextWriter writer = System.IO.File.AppendText("Inserts.log");
writer.WriteLine("{0}: Customer {1} Inserted.",
DateTime.Now, args.Row["CustomerID"]);
writer.Close();
}
}
protected static void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
...{
if (args.Status == UpdateStatus.ErrorsOccurred)
...{
args.Row.RowError = args.Errors.Message;
args.Status = UpdateStatus.SkipCurrentRow;
}
}
今天在使用gridview时出现一个低级错误.
朋友们我有个低级问题,我用GridView编辑功能时,查找不出里面的TextBox
string classname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
其实e.RowIndex怎么点都不出来,要怎么设置才能点出来啊
“System.Web.UI.WebControls.GridViewUpdatedEventArgs”并不包含“RowIndex”的定义
原因是我选用的是RowUpdated而非RowUpdating这两个事件是不同的....
里面RowUpdating才能有这个选项:RowIndex,RowUpdated则没有....
应用得太少....要多加练习了!!!
文章出处:DIY部落(http://www.diybl.com/course/1_web/webjs/200855/114463.html)