I have a tableadapter and I want to do something when the RowUpdated event is fired. I can't figure out where to put the code to add the handler to the event.
public partial class MyTableAdapter
{
void OnRowUpdated(object sender, System.Data.Odbc.OdbcRowUpdatedEventArgs e)
{
}
}
How do I get the code below to run when the TableAdapter is created?
Adapter.RowUpdated +=
new System.Data.Odbc.OdbcRowUpdatedEventHandler(OnRowUpdated);
I resolved this in 2 stages.
a. add partial class and extend table adapter
b. call a method in the beginning before using this adapter (say in main form after you create instance of it).
The code below is to resolve my particular issue with SQL CE to be able to update IDs on the table. However you can use the extended method to wrap the RowUpdated event and expose it to other classes (ie MyRowUpdated)
The extension
public partial class ScannedItemsTableAdapter
{
public void InitEvents()
{
this._adapter.RowUpdated += _adapter_RowUpdated;
}
void _adapter_RowUpdated(object sender, SqlCeRowUpdatedEventArgs e)
{
if (e.Status == UpdateStatus.Continue &&
e.StatementType == StatementType.Insert)
{
var pk = e.Row.Table.PrimaryKey;
pk[0].ReadOnly = false;
SqlCeCommand cmd = new SqlCeCommand("SELECT @@IDENTITY",
e.Command.Connection, e.Command.Transaction);
object id = (decimal)cmd.ExecuteScalar();
e.Row[pk[0]] = Convert.ToInt32(id);
e.Row.AcceptChanges();
}
}
}
The call in the main form:
tableAdapter = new ScannedItemsTableAdapter();
tableAdapter.Fill(ds.ScannedItems);
tableAdapter.InitEvents();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With