namespace RCRS.AdoNetEF.Library.Presentation.AdoNet20
{
    //------------------------------------------------------------------
    /// <summary>
    
/// バインディングする画面用インターフェース
    
/// </summary>
    //------------------------------------------------------------------
    interface IBindUI
    {
        //--------------------------------------------------------------
        /// <summary>
        
/// バインドするデータを取得するメソッドです。
        
/// </summary>
        
/// <returns></returns>
        //--------------------------------------------------------------
        bool GetData();
    }
}

using System.Data;
using System.Windows.Forms;

namespace RCRS.AdoNetEF.Library.Presentation.AdoNet20
{
    //--------------------------------------------------------------
    /// <summary>
    
/// DataViewとCurrencyManagerのペアセットです。
    
/// </summary>
    //--------------------------------------------------------------
    public class ViewSource
    {
        private DataView        dv = null;
        private CurrencyManager cm = null;

        //--------------------------------------------------------------
        /// <summary>
        
/// 新しいインスタンスを生成します。
        
/// </summary>
        
/// <param name="dv"></param>
        
/// <param name="cm"></param>
        //--------------------------------------------------------------
        public ViewSource(DataView dv, CurrencyManager cm)
        {
            this.dv = dv;
            this.cm = cm;
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// DataViewを取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        public DataView Dv
        {
            get { return dv; }
            set { dv = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// CurrencyManagerを取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        public CurrencyManager Cm
        {
            get { return cm; }
            set { cm = value; }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace RCRS.AdoNetEF.Library.Presentation.AdoNet20
{
    //-----------------------------------------------------------------------
    /// <summary>
    
/// バインド用ビジネスロジックです。
    
/// </summary>
    //-----------------------------------------------------------------------
    public class BizBind
    {
        //バインドエラー:テーブル
        private const string ERROR_BIND_TABLE     = "ERROR:BIND:TABLE";
        //バインドエラー:フィールド
        private const string ERROR_BIND_DATAFIELD = "ERROR:BIND:DATAFIELD";

        //-----------------------------------------------------------------------
        /// <summary>
        
/// dsからViewSources(Dictionary<string, ViewSource>)を作成します。
        
/// </summary>
        
/// <param name="control"></param>
        
/// <param name="ds"></param>
        
/// <returns></returns>
        //-----------------------------------------------------------------------
        public static Dictionary<string, ViewSource> CreateViewSources(Control control, DataSet ds)
        {
            Dictionary<string, ViewSource> viewSources = new Dictionary<string, ViewSource>();
            if (ds != null)
            {
                foreach (DataTable dt in ds.Tables)
                {
                    DataView        dv = new DataView(dt);
                    CurrencyManager cm = (CurrencyManager)control.BindingContext[dv];
                    ViewSource      vs = new ViewSource(dv, cm);

                    viewSources.Add(dv.Table.TableName, vs); //テーブル名をキー値に設定
                }
            }
            return viewSources;
        }

        //-----------------------------------------------------------------------
        /// <summary>
        
/// バインディングコントロールリストを登録します。
        
/// Control内を走査
        
/// </summary>
        
/// <param name="top">このコントロール内のバインディングコントロールを登録します。</param>
        
/// <param name="bindControls">バインディングコントロールリスト</param>
        
/// <returns></returns>
        //-----------------------------------------------------------------------
        public static List<KeyValuePair<string, Control>> CreateBindingControlList(Control top, List<KeyValuePair<string, Control>> bindControls)
        {
            if ((top.GetType()    == typeof(BTextBox))      || (top.GetType() == typeof(BComboBox))
                || (top.GetType() == typeof(BDataGridView)) || (top.GetType() == typeof(BLabel))
                || (top.GetType() == typeof(BCheckBox))     || (top.GetType() == typeof(BRichTextBox))
                || (top.GetType() == typeof(BTextComboBox)) || (top.GetType() == typeof(BDecimalBox)))
            {
                KeyValuePair<string, Control> cPair = new KeyValuePair<string, Control>(top.Name, top);
                //bindControlに追加されていない場合は追加
                if (bindControls.IndexOf(cPair) == -1)
                    bindControls.Add(cPair);
            }
            //子要素の走査
            foreach (Control c in top.Controls)
                bindControls = CreateBindingControlList(c, bindControls);

            return bindControls;
        }

        //-----------------------------------------------------------------------
        /// <summary>
        
/// コントロール内の各コントロールに対し、バインディングコントロールリストから削除します。
        
/// </summary>
        
/// <param name="top">このコントロール内のバインディングコントロールを登録します。</param>
        
/// <param name="bindControls">バインディングコントロールリスト</param>
        //-----------------------------------------------------------------------
        public static void RemoveBindingControlList(Control top, List<KeyValuePair<string, Control>> bindControls)
        {
            if ((top.GetType()    == typeof(BTextBox))      || (top.GetType() == typeof(BComboBox))
                || (top.GetType() == typeof(BDataGridView)) || (top.GetType() == typeof(BLabel))
                || (top.GetType() == typeof(BCheckBox))     || (top.GetType() == typeof(BRichTextBox))
                || (top.GetType() == typeof(BTextComboBox)) || (top.GetType() == typeof(BDecimalBox)))
            {
                KeyValuePair<string, Control> cPair = new KeyValuePair<string, Control>(top.Name, top);
                int indx = bindControls.IndexOf(cPair);
                if (0 <= indx)
                {
                    //バインドのクリア
                    cPair.Value.DataBindings.Clear();
                    //リストから削除
                    bindControls.Remove(cPair);
                }
            }
            //子要素の走査
            foreach (Control c in top.Controls)
                RemoveBindingControlList(c, bindControls);
        }

        //-----------------------------------------------------------------------
        /// <summary>
        
/// バインドを実行します。
        
/// </summary>
        
/// <param name="viewSources">データソースリスト</param>
        
/// <param name="bindControls">バインディングコントロールリスト</param>
        //-----------------------------------------------------------------------
        public static void Bind(Dictionary<string, ViewSource> viewSources, List<KeyValuePair<string, Control>> bindControls)
        {
            foreach (KeyValuePair<string, Control> ctrl in bindControls)
            {
                if ((checkBindSettings(viewSources, ctrl.Value)) && (ctrl.Value.DataBindings.Count == 0))
                {
                    if (ctrl.Value.GetType() == typeof(BDataGridView))
                    {
                        BDataGridView tmp = (BDataGridView)ctrl.Value;
                        tmp.DataSource = viewSources[tmp.TableName].Dv;
                    }
                    else if (ctrl.Value.GetType() == typeof(BLabel))
                    {
                        BLabel tmp = (BLabel)ctrl.Value;
                        ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField);
                    }
                    else if (ctrl.Value.GetType() == typeof(BTextBox))
                    {
                        BTextBox tmp = (BTextBox)ctrl.Value;
                        if (tmp.FormatString == string.Empty)
                            ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField);
                        else //書式が設定されている場合
                            ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField, true, DataSourceUpdateMode.OnValidation, "", tmp.FormatString);
                    }
                    else if (ctrl.Value.GetType() == typeof(BRichTextBox))
                    {
                        BRichTextBox tmp = (BRichTextBox)ctrl.Value;
                        ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField);
                    }
                    else if (ctrl.Value.GetType() == typeof(BDecimalBox))
                    {
                        BDecimalBox tmp = (BDecimalBox)ctrl.Value;
                        if (tmp.FormatString == string.Empty)
                            ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField);
                        else //書式が設定されている場合
                            ctrl.Value.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField, true, DataSourceUpdateMode.OnValidation, "", tmp.FormatString);
                    }
                    else if (ctrl.Value.GetType() == typeof(BComboBox))
                    {
                        BComboBox tmp = (BComboBox)ctrl.Value;
                        if (tmp.TableName != null && tmp.DataField != null)
                        {
                            if (viewSources[tmp.TableName].Dv.Table.Columns.Contains(tmp.DataField))
                                cmbBinding((ComboBox)ctrl.Value, viewSources[tmp.ListTableName].Dv, viewSources[tmp.TableName].Dv, tmp.DisplayField, tmp.ValueField, tmp.DataField);
                        }
                        else
                            cmbBinding((ComboBox)ctrl.Value, viewSources[tmp.ListTableName].Dv, null, tmp.DisplayField, tmp.ValueField, "");
                    }
                    else if (ctrl.Value.GetType() == typeof(BCheckBox))
                    {
                        BCheckBox tmp = (BCheckBox)ctrl.Value;
                        ctrl.Value.DataBindings.Add("Checked", viewSources[tmp.TableName].Dv, tmp.DataField);
                    }
                    else if (ctrl.Value.GetType() == typeof(BTextComboBox))
                    {
                        BTextComboBox tmp = (BTextComboBox)ctrl.Value;
                        tmp.ValueType = viewSources[tmp.ListTableName].Dv.Table.Columns[tmp.ValueField].DataType; //値の型を設定(テキストからコンボへのキャスト用)
                        tmp.txtBox.DataBindings.Add("Text", viewSources[tmp.TableName].Dv, tmp.DataField, true, DataSourceUpdateMode.OnPropertyChanged);
                        cmbBinding(tmp.cmbBox, viewSources[tmp.ListTableName].Dv, viewSources[tmp.TableName].Dv, tmp.DisplayField, tmp.ValueField, tmp.DataField);
                    }
                    Console.WriteLine(ctrl.Key + ":" + ctrl.Value.DataBindings.Count.ToString()); 
                }
            }
        }
        
        //-------------------------------------------------------------------------
        /// <summary>
        
/// バインディングプロパティ設定をチェックします。
        
/// </summary>
        
/// <param name="viewSources">バインドするViewSources</param>
        
/// <param name="ctrl">バインディングコントロール</param>
        
/// <returns>true:OK false:ERROR</returns>
        //-------------------------------------------------------------------------
        private static bool checkBindSettings(Dictionary<string, ViewSource> viewSources, Control ctrl)
        {
            bool   result        = false;
            string tableName     = string.Empty;
            string dataField     = string.Empty;
            string listTableName = string.Empty;
            string displayField  = string.Empty;
            string valueField    = string.Empty;

            //設定値取得
            if (ctrl.GetType() == typeof(BDataGridView))
            {
                BDataGridView tmp = (BDataGridView)ctrl;
                tableName         = tmp.TableName;
            }
            else if (ctrl.GetType() == typeof(BLabel))
            {
                BLabel tmp = (BLabel)ctrl;
                tableName  = tmp.TableName;
                dataField  = tmp.DataField;
            }
            else if (ctrl.GetType() == typeof(BTextBox))
            {
                BTextBox tmp = (BTextBox)ctrl;
                tableName    = tmp.TableName;
                dataField    = tmp.DataField;
            }
            else if (ctrl.GetType() == typeof(BDecimalBox))
            {
                BDecimalBox tmp = (BDecimalBox)ctrl;
                tableName = tmp.TableName;
                dataField = tmp.DataField;
            }
            else if (ctrl.GetType() == typeof(BRichTextBox))
            {
                BRichTextBox tmp = (BRichTextBox)ctrl;
                tableName        = tmp.TableName;
                dataField        = tmp.DataField;
            }
            else if (ctrl.GetType() == typeof(BComboBox))
            {
                BComboBox tmp = (BComboBox)ctrl;
                tableName     = tmp.TableName;
                dataField     = tmp.DataField;
                listTableName = tmp.ListTableName;
                displayField  = tmp.DisplayField;
                valueField    = tmp.ValueField;
            }
            else if (ctrl.GetType() == typeof(BCheckBox))
            {
                BCheckBox tmp = (BCheckBox)ctrl;
                tableName     = tmp.TableName;
                dataField     = tmp.DataField;
            }
            else if (ctrl.GetType() == typeof(BTextComboBox))
            {
                BTextComboBox tmp = (BTextComboBox)ctrl;
                tableName         = tmp.TableName;
                dataField         = tmp.DataField;
                listTableName     = tmp.ListTableName;
                displayField      = tmp.DisplayField;
                valueField        = tmp.ValueField;
            }

            //設定エラーチェック
            if (ctrl.GetType() == typeof(BDataGridView) || ctrl.GetType() == typeof(BCheckBox))
            {
                if ((!string.IsNullOrEmpty(tableName)) && (viewSources.Keys.Contains(tableName)))
                    result = true//バインド対象
            }
            else if ((ctrl.GetType() == typeof(BLabel)) || (ctrl.GetType() == typeof(BTextBox)) || (ctrl.GetType() == typeof(BRichTextBox)) || (ctrl.GetType() == typeof(BDecimalBox)))
            {
                //テーブル名とフィールド名が設定されている場合はバインドする。
                if (!string.IsNullOrEmpty(tableName) && (!string.IsNullOrEmpty(dataField)))
                {
                    //テーブルの有無チェック
                    if (viewSources.Keys.Contains(tableName))
                    {
                        //フィールドの有無チェック
                        if (viewSources[tableName].Dv.Table.Columns.Contains(dataField))
                        {
                            ctrl.BackColor = Color.LemonChiffon;
                            result         = true//バインド対象
                        }
                        else
                        {
                            ctrl.Text      = ERROR_BIND_DATAFIELD;
                            ctrl.BackColor = Color.Red;
                        }
                    }
                    else
                    {
                        ctrl.Text      = ERROR_BIND_TABLE;
                        ctrl.BackColor = Color.Red;
                    }
                }
            }
            else if ((ctrl.GetType() == typeof(BComboBox)) || (ctrl.GetType() == typeof(BTextComboBox)))
            {
                //リストテーブルと表示列名・値列名が正しくセットされている場合はバインドを行う。
                if ((!string.IsNullOrEmpty(listTableName)) && (viewSources.Keys.Contains(listTableName)))
                {
                    //リストのフィールド有無チェック
                    if ((viewSources[listTableName].Dv.Table.Columns.Contains(valueField))
                        && (viewSources[listTableName].Dv.Table.Columns.Contains(displayField)))
                    {
                        ctrl.BackColor = Color.LemonChiffon;
                        result         = true;
                    }
                    else
                    {
                        ctrl.Text      = ERROR_BIND_DATAFIELD;
                        ctrl.BackColor = Color.Red;
                    }
                }
                else
                {
                    ctrl.Text      = ERROR_BIND_TABLE;
                    ctrl.BackColor = Color.Red;
                }
            }
            return result;
        }

        //-------------------------------------------------------------------------
        /// <summary>
        
/// コンボボックスを指定したリストデータとバインディングします。
        
/// </summary>
        
/// <param name="cmb">コンボボックス</param>
        
/// <param name="listDataSource">リストテーブル</param>
        
/// <param name="dataSource">テーブル</param>
        
/// <param name="displayMember">リストテーブルの表示列</param>
        
/// <param name="valueMember">リストテーブルのバインド列</param>
        
/// <param name="dataMember">テーブルのバインド列</param>
        //-------------------------------------------------------------------------
        private static void cmbBinding(ComboBox cmb, object listDataSource, object dataSource, string displayMember, string valueMember, string dataMember)
        {
            cmb.DisplayMember = displayMember;
            cmb.ValueMember   = valueMember;
            cmb.DataSource    = listDataSource;
            if ((dataSource != null) && (dataMember != ""))
                cmb.DataBindings.Add("SelectedValue", dataSource, dataMember);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace RCRS.AdoNetEF.Library.Presentation.AdoNet20
{
    //--------------------------------------------------------------
    /// <summary>
    
/// 各画面の基本クラスです。
    
/// ログイン情報を保持します。
    
/// </summary>
    //--------------------------------------------------------------
    public partial class FrmBase : Form, IBindUI
    {
        /// <summary></summary>
        protected DataSet ds;
        /// <summary></summary>
        protected Dictionary<string, ViewSource> ViewSources = new Dictionary<string, ViewSource>();
        /// <summary></summary>
        protected List<KeyValuePair<string, Control>> BindControls = new List<KeyValuePair<string, Control>>();

        //--------------------------------------------------------------
        /// <summary>
        
/// 新しいインスタンスを生成します。
        
/// </summary>
        //--------------------------------------------------------------
        public FrmBase()
        {
            InitializeComponent();
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// ロード時に発生します。
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        //--------------------------------------------------------------
        private void FrmBase_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;
            //バインドデータを取得します。(派生先を実行)
            GetData();
            //ViewSourcesを作成します。
            ViewSources  = BizBind.CreateViewSources(this, ds);
            BindControls = BizBind.CreateBindingControlList(this, BindControls);
            BizBind.Bind(ViewSources, BindControls);
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// インスタンス生成時のバインドデータ取得処理を記述します。
        
/// </summary>
        
/// <returns></returns>
        //--------------------------------------------------------------
        public virtual bool GetData()
        {
            return false;
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// キーダウン時発生します。
        
/// Enterキーでのフォーカス制御を実装しています。
        
/// 適用する場合は派生先のクラスにて、KeyPreviewをTrueに設定して下さい。
        
/// 但し、その場合DataGridViewにフォーカスが当たっている場合であってもフォーカス移動されます。
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        //--------------------------------------------------------------
        private void FrmBase_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //DataGridView系統、RichTextBox系統のコントロールは除外されます。
                if ((typeof(BDataGridView) != ActiveControl.GetType()) && (typeof(DataGridView) != ActiveControl.GetType())
                    && (typeof(BRichTextBox) != ActiveControl.GetType()) && (typeof(RichTextBox) != ActiveControl.GetType()))
                {
                    // 次のコントロールへフォーカス移動
                    SelectNextControl(ActiveControl, !e.Shift, truetruetrue);
                    e.Handled = true;
                }
            }
        }
    }
}

using System.ComponentModel;
using System.Windows.Forms;

namespace RCRS.AdoNetEF.Library.Presentation.AdoNet20
{
    //--------------------------------------------------------------
    /// <summary>
    
/// バインディング用ComboBoxユーザーコントロールです。
    
/// </summary>
    //--------------------------------------------------------------
    public partial class BComboBox : ComboBox, IBindCtrl
    {
        private string tableName     = string.Empty;
        private string dataField     = string.Empty;
        private string listTableName = string.Empty;
        private string valueField    = string.Empty;
        private string displayField  = string.Empty;

        //--------------------------------------------------------------
        /// <summary>
        
/// テーブル名を取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        [DefaultValue("")]
        [Description("テーブル名")]
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// テーブルのバインド列を取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        [DefaultValue("")]
        [Description("テーブルのバインド列")]
        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// リストテーブル名を取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        [DefaultValue("")]
        [Description("リストテーブル名")]
        public string ListTableName
        {
            get { return listTableName; }
            set { listTableName = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// リストテーブルのバインド列を取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        [DefaultValue("")]
        [Description("リストテーブルのバインド列")]
        public string ValueField
        {
            get { return valueField; }
            set { valueField = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// リストテーブルの表示列を取得・設定します。
        
/// </summary>
        //--------------------------------------------------------------
        [DefaultValue("")]
        [Description("リストテーブルの表示列")]
        public string DisplayField
        {
            get { return displayField; }
            set { displayField = value; }
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// 新しいインスタンスを生成します。
        
/// </summary>
        //--------------------------------------------------------------
        public BComboBox()
        {
            InitializeComponent();
        }

        //--------------------------------------------------------------
        /// <summary>
        
/// 描画時発生します。
        
/// </summary>
        
/// <param name="pe"></param>
        //--------------------------------------------------------------
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}



            ViewSources  = BizBind.CreateViewSources(this, ds);
            BindControls = BizBind.CreateBindingControlList(this, BindControls);
            BizBind.Bind(ViewSources, BindControls);






posted on 2016-12-23 14:29 Ying-er 阅读(178) 评论(0)  编辑  收藏 所属分类: .Net

只有注册用户登录后才能发表评论。


网站导航: