私のアプリケーションには、強く型付けされたデータグリッドビュー、強く型付けされたバインディングソース、強く型付けされたテーブルアダプタからなるいくつかのフォームが含まれています。イベントを異なる型の厳密に型指定されたデータセットにバインドするにはどうすればよいですか?
私は現在の行を残したり、フォーカスをデータグリッドやフォームから外したり、フォームを閉じたりするたびに、データベースを更新するために各フォームでいくつかのコードを使用しています。
このコードはどちらの場合も同じです。したがって、これらのフォームのすべてが継承できるフォームのサブクラスを作成します。
しかし、厳密に型指定されたデータオブジェクトは、すべてバインドしたいイベントや呼び出すメソッドを公開しないコンポーネントから継承します。
私はイベントへのアクセスを得るの見ることができる唯一の方法は、使用することです: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;
}
}