How do I Add an Event Handler To the RowUpdated event in a table adapter

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();

I have a possible alternative.

Whilst one can subscribe to the tableadapters underlying adapter in the tableadapter partial class I find you cannot easily initialise it without having to remember to call an initialisation of your own.

However I found that I can subscribe to a tableadapters.adapter.rowupdated event directly from within code using the tableadpater as below

AddHandler Me.TA_Case_Transactions.Adapter.RowUpdated, AddressOf Transaction_Row_Written

  Private Sub Transaction_Row_Written(p_Sender As Object, p_E As SqlRowUpdatedEventArgs)
        beep
    End Sub

"The beep is so I can add a breakpoint for testing"

Since I would have to subscribe to the event anyway I think this is more intuitive and doesnt require any "remembering"


EDIT: None of the steps suggested below are useful, apparently. I'm keeping this answer here purely so that anyone else trying to answer it doesn't come up with the same suggestions.


Are you creating the TableAdapter in the designer? If so, can you not just click on the Events part of the properties page, and type "OnRowUpdated" into the RowUpdated entry?

If you're creating the adapter explicitly in your code, just add the call yourself.

Alternatively, does your partial class have a constructor which is being called? Again, if so, you can add the call there.

EDIT: Okay, so presumably this is being used in a particular page or form - can you add it after the InitializeComponent method of that page/form?

链接地址: http://www.djcxy.com/p/46906.html

上一篇: 如何在GWT(Google Web Toolkit)项目中包含外部jar?

下一篇: 如何添加事件处理程序到表适配器中的RowUpdated事件