我如何添加一个事件处理RowUpdated事件表中的适配器 [英] How do I Add an Event Handler To the RowUpdated event in a table adapter

查看:257
本文介绍了我如何添加一个事件处理RowUpdated事件表中的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableAdapter,我希望在RowUpdated事件被激发做一些事情。我想不出哪里把代码添加处理程序的事件。

 公共部分类MyTableAdapter 
{
无效OnRowUpdated(对象发件人,System.Data.Odbc.OdbcRowUpdatedEventArgs E)
{
}
}

我如何得到下面的代码时创建的TableAdapter运行?

  Adapter.RowUpdated + = 
新System.Data.Odbc.OdbcRowUpdatedEventHandler(OnRowUpdated);


解决方案

我在第2阶段解决了这一点。



一。增加部分类和扩展表适配器



乙。使用该适配器之前调用开始的方法(比如在你创建它的实例后主要形式)。



下面的代码是解决我的具体问题与SQL CE要能在桌子上更新的ID。但是,您可以使用扩展的方法来包装RowUpdated事件,并暴露在其他类(即MyRowUpdated)



扩展

 公共部分类ScannedItemsTableAdapter 
{
公共无效InitEvents()
{
this._adapter.RowUpdated + = _adapter_RowUpdated;
}

无效_adapter_RowUpdated(对象发件人,SqlCeRowUpdatedEventArgs E)
{
如果(e.Status == UpdateStatus.Continue&放大器;&安培;
é .StatementType == StatementType.Insert)
{
无功PK = e.Row.Table.PrimaryKey;
PK [0] = .ReadOnly虚假的;

SqlCeCommand CMD =新SqlCeCommand(SELECT @@ IDENTITY,
e.Command.Connection,e.Command.Transaction);

对象ID =(十进制)cmd.ExecuteScalar();

e.Row [PK [0]] = Convert.ToInt32(ID);
e.Row.AcceptChanges();
}
}
}



在主窗体调用

 的TableAdapter =新ScannedItemsTableAdapter(); 
tableAdapter.Fill(ds.ScannedItems);
tableAdapter.InitEvents();


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

这篇关于我如何添加一个事件处理RowUpdated事件表中的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆