开发者

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开发者_Go百科 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();


it has been a while since i last used this stuff, but if i remember correctly you should be able to override EndInit() in your partial class and add init code like adding your event handler there ...


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://windowsclient.net/learn/video.aspx?v=14625 if you look at this video you can see, that you just double click on something in the dataset designer and the event gets generated and wired up for you....but this works only for VB programmers :)) sux


Override endinitin your C# class and fire event handler from there

    public override void EndInit()
    {
        base.EndInit();
        Adapter.RowUpdated += 
                   new System.Data.Odbc.OdbcRowUpdatedEventHandler(OnRowUpdated);

    }

    void OnRowUpdated(object sender, System.Data.Odbc.OdbcRowUpdatedEventArgs e)
    {

    }

Hope that helps................


// Assumes that connection is a valid SqlConnection object.

SqlDataAdapter custAdapter = new SqlDataAdapter(
  "SELECT CustomerID, CompanyName FROM Customers", connection);

// Add handlers.

custAdapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
custAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);

// Set DataAdapter command properties, fill DataSet, modify DataSet.


custAdapter.Update(custDS, "Customers");

// Remove handlers.

custAdapter.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating);
custAdapter.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);

protected static void OnRowUpdating(
  object sender, SqlRowUpdatingEventArgs args)
{
  if (args.StatementType == StatementType.Delete)
  {
    System.IO.TextWriter tw = System.IO.File.AppendText("Deletes.log");
    tw.WriteLine(
      "{0}: Customer {1} Deleted.", DateTime.Now, 
       args.Row["CustomerID", DataRowVersion.Original]);
    tw.Close();
  }
}

protected static void OnRowUpdated(
  object sender, SqlRowUpdatedEventArgs args)
{
  if (args.Status == UpdateStatus.ErrorsOccurred)
  {
    args.Row.RowError = args.Errors.Message;

    args.Status = UpdateStatus.SkipCurrentRow;
  }
}


Here's an easy way to do it, using the Designer. Right-click on the dataset (the .xsd file) and select View Code. Just like with a form, it's not going to open the designer code. It's going to open your code. Here's your code:

namespace TimeTrack.TimeTrackDataSetTableAdapters
{
    public partial class TimeSlipsTableAdapter
    {
        public void CustomSetup()
        {
            Adapter.RowUpdated += Adapter_RowUpdated;
        }

        private void Adapter_RowUpdated(object sender, OleDbRowUpdatedEventArgs e)
        {
            // whatever you want to do when RowUpdated fires
        }
    }
}

In your form, don't forget to

    public TimeEntry()
    {
        InitializeComponent();
        timeSlipsTableAdapter.CustomSetup();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜