原系统在BusinessTool中有30来个绑定数据的方法,有必要对方法进行一次整理。最新的设想是这样的,只提供一个绑定数据的方法,方法有两个参数:DataTable、需绑定的控件。绑定控件的类型可以有许多,ComboBoxEdit、RepositoryItemLookUpEdit、ImageComboBoxEdit、LookUpEdit等等。这些通过方法的多态实现。
具体的调用代码如下:
BindDataHelper.BindDropDownList(ClientCacheHelper.GetWordsByID(WordsType.开账分类), LueCH);
第一参数传递的是一个DataTable。如果该DataTable为系统词语、用户词语等常用词语可以从缓存中获得,否则自己调用应用层获取DataTable。注意由于希望传递的数据量越小越好,所以DataTable有对应的列长度限制,如果长度为2则绑定Key和Value。否则Key和Value相同。
用了Linq以后会用List作为参数传递取代对应的DataTable,这版还是用DataTable。
下面为数据绑定类的代码实现:
//********************************************************
//创建日期:<创建日期,2012-05-28>
//创建作者:<张易,zhangyi@shanghai3h.com>
//功能说明: 绑定数据帮助类
//********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
namespace CSMS2.Infrastructure.Helpers
{
public static class BindDataHelper
{
/// <summary>
/// 绑定LookUpEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, LookUpEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.DataSource = dt;
lst.Properties.DisplayMember = dt.Columns[0].ColumnName;
if (dt.Columns.Count == 1)
{
lst.Properties.ValueMember = dt.Columns[0].ColumnName;
}
else
{
lst.Properties.ValueMember = dt.Columns[1].ColumnName;
}
lst.Properties.Columns.Clear();
lst.Properties.Columns.Add(new LookUpColumnInfo(dt.Columns[0].ColumnName, 80));
lst.Properties.ShowHeader = false;
lst.Properties.ShowFooter = false;
lst.Properties.NullText = "";
lst.Properties.PopupFormMinSize = new System.Drawing.Size(10, 10);
lst.Properties.PopupWidth = lst.Width - 3;
lst.Properties.DropDownRows = dt.Rows.Count <= 6 ? dt.Rows.Count : 6;
if (dt.Rows.Count > 0)
{
lst.Properties.ForceInitialize();
lst.ItemIndex = 0;
}
}
}
/// <summary>
/// 绑定RepositoryItemLookUpEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, RepositoryItemLookUpEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.DataSource = dt;
lst.DisplayMember = dt.Columns[0].ColumnName;
if (dt.Columns.Count == 1)
{
lst.ValueMember = dt.Columns[0].ColumnName;
}
else
{
lst.ValueMember = dt.Columns[1].ColumnName;
}
lst.Columns.Clear();
lst.Columns.Add(new LookUpColumnInfo(dt.Columns[0].ColumnName, 80));
lst.ShowHeader = false;
lst.ShowFooter = false;
lst.NullText = "";
lst.PopupFormMinSize = new System.Drawing.Size(10, 10);
lst.DropDownRows = dt.Rows.Count <= 6 ? dt.Rows.Count : 6;
}
}
/// <summary>
/// 绑定ComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, DevExpress.XtraEditors.ComboBoxEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Properties.Items.Add(item);
}
if (lst.Properties.Items.Count > 0)
lst.SelectedIndex = 0;
}
}
/// <summary>
/// 绑定ImageComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, ImageComboBoxEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ImageComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ImageComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ImageComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Properties.Items.Add(item);
}
if (lst.Properties.Items.Count > 0)
lst.SelectedIndex = 0;
}
}
/// <summary>
/// 绑定ImageComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, RepositoryItemImageComboBox lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ImageComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ImageComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ImageComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Items.Add(item);
}
}
}
public class ComboBoxItem
{
private string _value;
private string _text;
public ComboBoxItem(string text, string value)
{
_value = value;
_text = text;
}
public override string ToString()
{
return _text;
}
public string Value
{
get
{
return _value;
}
}
}
}
}
//创建日期:<创建日期,2012-05-28>
//创建作者:<张易,zhangyi@shanghai3h.com>
//功能说明: 绑定数据帮助类
//********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
namespace CSMS2.Infrastructure.Helpers
{
public static class BindDataHelper
{
/// <summary>
/// 绑定LookUpEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, LookUpEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.DataSource = dt;
lst.Properties.DisplayMember = dt.Columns[0].ColumnName;
if (dt.Columns.Count == 1)
{
lst.Properties.ValueMember = dt.Columns[0].ColumnName;
}
else
{
lst.Properties.ValueMember = dt.Columns[1].ColumnName;
}
lst.Properties.Columns.Clear();
lst.Properties.Columns.Add(new LookUpColumnInfo(dt.Columns[0].ColumnName, 80));
lst.Properties.ShowHeader = false;
lst.Properties.ShowFooter = false;
lst.Properties.NullText = "";
lst.Properties.PopupFormMinSize = new System.Drawing.Size(10, 10);
lst.Properties.PopupWidth = lst.Width - 3;
lst.Properties.DropDownRows = dt.Rows.Count <= 6 ? dt.Rows.Count : 6;
if (dt.Rows.Count > 0)
{
lst.Properties.ForceInitialize();
lst.ItemIndex = 0;
}
}
}
/// <summary>
/// 绑定RepositoryItemLookUpEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, RepositoryItemLookUpEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.DataSource = dt;
lst.DisplayMember = dt.Columns[0].ColumnName;
if (dt.Columns.Count == 1)
{
lst.ValueMember = dt.Columns[0].ColumnName;
}
else
{
lst.ValueMember = dt.Columns[1].ColumnName;
}
lst.Columns.Clear();
lst.Columns.Add(new LookUpColumnInfo(dt.Columns[0].ColumnName, 80));
lst.ShowHeader = false;
lst.ShowFooter = false;
lst.NullText = "";
lst.PopupFormMinSize = new System.Drawing.Size(10, 10);
lst.DropDownRows = dt.Rows.Count <= 6 ? dt.Rows.Count : 6;
}
}
/// <summary>
/// 绑定ComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, DevExpress.XtraEditors.ComboBoxEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Properties.Items.Add(item);
}
if (lst.Properties.Items.Count > 0)
lst.SelectedIndex = 0;
}
}
/// <summary>
/// 绑定ImageComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, ImageComboBoxEdit lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Properties.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ImageComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ImageComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ImageComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Properties.Items.Add(item);
}
if (lst.Properties.Items.Count > 0)
lst.SelectedIndex = 0;
}
}
/// <summary>
/// 绑定ImageComboBoxEdit
/// </summary>
/// <param name="dt"></param>
/// <param name="lst"></param>
public static void BindDropDownList(DataTable dt, RepositoryItemImageComboBox lst)
{
if (dt.Columns.Count >= 1 && dt.Columns.Count <= 2)
{
lst.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ImageComboBoxItem item;
if (dt.Columns.Count == 1)
{
item = new ImageComboBoxItem(row[0].ToString(), row[1].ToString());
}
else
{
item = new ImageComboBoxItem(row[0].ToString(), row[0].ToString());
}
lst.Items.Add(item);
}
}
}
public class ComboBoxItem
{
private string _value;
private string _text;
public ComboBoxItem(string text, string value)
{
_value = value;
_text = text;
}
public override string ToString()
{
return _text;
}
public string Value
{
get
{
return _value;
}
}
}
}
}