2009-05-26 12 views
1

私のアプリケーションには、強く型付けされたデータグリッドビュー、強く型付けされたバインディングソース、強く型付けされたテーブルアダプタからなるいくつかのフォームが含まれています。イベントを異なる型の厳密に型指定されたデータセットにバインドするにはどうすればよいですか?

私は現在の行を残したり、フォーカスをデータグリッドやフォームから外したり、フォームを閉じたりするたびに、データベースを更新するために各フォームでいくつかのコードを使用しています。

このコードはどちらの場合も同じです。したがって、これらのフォームのすべてが継承できるフォームのサブクラスを作成します。

しかし、厳密に型指定されたデータオブジェクトは、すべてバインドしたいイベントや呼び出すメソッドを公開しないコンポーネントから継承します。

私はイベントへのアクセスを得るの見ることができる唯一の方法は、使用することです:Type(string Name).GetEvent(string EventName).AddEventHandler(object Target,Delegate Handler)

同様に、私は強く型付けされたテーブルアダプタのUpdateメソッドを呼び出したい、とType(string Name).GetMethod(String name, Type[] params).Invoke(object target, object[] params)を使用しています。

正常に動作しますが、非常に重いと思われます。より良い方法がありますか?ここで

はメインクラスのために私のコードです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.SqlClient; 
using System.ComponentModel; 

namespace MyApplication 
{ 
    public class AutoSaveDataGridForm: Form 
    { 
     private DataRow PreviousRow; 

     public Component Adapter 
     { 
      private get; 
      set; 
     } 


     private Component dataGridView; 
     public Component DataGridView 
     { 
      private get 
      { 
       return dataGridView; 
      } 
      set 
      { 
       dataGridView = value; 
       Type t = dataGridView.GetType(); 
       t.GetEvent("Leave").AddEventHandler(dataGridView, new EventHandler(DataGridView_Leave)); 

      } 
     } 
     private Component bindingSource; 
     public Component BindingSource 
     { 
      private get 
      { 
       return bindingSource; 
      } 
      set 
      { 
       bindingSource = value; 
       Type t = bindingSource.GetType(); 
       t.GetEvent("PositionChanged").AddEventHandler(bindingSource, new EventHandler(BindingSource_PositionChanged)); 


      } 
     } 



     protected void Save() 
     { 
      if (PreviousRow != null && PreviousRow.RowState != DataRowState.Unchanged) 
      { 
       Type t = Adapter.GetType(); 
       t.GetMethod("Update", new Type[] { typeof(DataRow[]) }).Invoke(Adapter, new object[] { new DataRow[] { PreviousRow } }); 

      } 

     } 


     private void BindingSource_PositionChanged(object sender, EventArgs e) 
     { 
      BindingSource bindingSource = sender as BindingSource; 
      DataRowView CurrentRowView = bindingSource.Current as DataRowView; 
      DataRow CurrentRow = CurrentRowView.Row; 
      if (PreviousRow != null && PreviousRow != CurrentRow) 
      { 
       Save(); 
      } 
      PreviousRow = CurrentRow; 

     } 

     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      // 
      // AutoSaveDataGridForm 
      // 
      this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.AutoSaveDataGridForm_FormClosed); 
      this.Leave += new System.EventHandler(this.AutoSaveDataGridForm_Leave); 
      this.ResumeLayout(false); 

     } 

     private void DataGridView_Leave(object sender, EventArgs e) 
     { 
      Save(); 
     } 

     private void AutoSaveDataGridForm_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      Save(); 
     } 

     private void AutoSaveDataGridForm_Leave(object sender, EventArgs e) 
     { 
      Save(); 
     } 


    } 
} 

そして、ここではそれを実装(部分)の形である:

public partial class FileTypesInherited :AutoSaveDataGridForm 
{ 
    public FileTypesInherited() 
    { 
     InitializeComponent(); 
    } 

    private void FileTypesInherited_Load(object sender, EventArgs e) 
    { 
     // TODO: This line of code loads data into the 'sharedFoldersInformationV2DataSet.tblFileTypes' table. You can move, or remove it, as needed. 
     this.tblFileTypesTableAdapter.Fill(this.sharedFoldersInformationV2DataSet.tblFileTypes); 
     this.BindingSource = tblFileTypesBindingSource; 
     this.Adapter = tblFileTypesTableAdapter; 
     this.DataGridView = tblFileTypesDataGridView; 

    } 

} 

答えて

0

これは、あなたが「MustInherit」ベースを使用することができますどのようにdemontrastes非公開のプロパティにアクセスする目的で、型付きデータセットのクラスです。

"System.ComponentModel.Component"の代わりに、型指定された各TableAdapterの "BaseClass"として設定します。 "MustInherit/MustOverride"(C#の "Abstract")を使用すると、それ以外の方法ではアクセスできないプロパティにアクセスできます。あなたはあなたが必要な「MustOverride」プロパティをオーバーライドする必要がありますBaseClassのを継承するように設定してテーブルアダプタのそれぞれについて

Public MustInherit Class SuperTableAdapter 
    Inherits System.ComponentModel.Component 

    Public MustOverride ReadOnly Property MyCommandCollection As Data.SqlClient.SqlCommand() 

    Public Sub New() 
     MyBase.New() 
     'With the command collection exposed, you can replace it with your own. You can do the same for events.' 

     For i = 0 To MyCommandCollection.Length - 1 
      Dim myspecialCommand As New Data.SqlClient.SqlCommand() 
      MyCommandCollection(i) = myspecialCommand 
     Next 
    End Sub 
End Class 

。それがなければ、それはコンパイルされません。コードを追加するが、TableAdapter基本クラスを設定しないと、コンパイルされません。それは良いことです。それはあなたが正しいことを確実にします。

Namespace DataSet1TableAdapters 
    Partial Public Class Table1TableAdapter 
     Public Overrides ReadOnly Property MyCommandCollection As System.Data.SqlClient.SqlCommand() 
      Get 
       Return Me.CommandCollection 
      End Get 
     End Property 
    End Class 

    Partial Public Class Table2TableAdapter 
     Public Overrides ReadOnly Property MyCommandCollection As System.Data.SqlClient.SqlCommand() 
      Get 
       Return Me.CommandCollection 
      End Get 
     End Property 
    End Class 
End Namespace 

ここで、あらゆる種類の特別なコードをSuperTableAdapterに入れることができます。公開されていないものにアクセスする必要がある場合は、「MustOverride」を使用して利用可能であることを保証してください。

関連する問題