65.9K
CodeProject 正在变化。 阅读更多。
Home

数据绑定组合框

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (11投票s)

2004年12月5日

CPOL

2分钟阅读

viewsIcon

113648

downloadIcon

609

本文提供了一个易于与数据源一起使用的组合框。

Sample Image - DataComboBox.png

引言

那么,我为什么制作这个控件呢? 许多软件开发公司为小型企业生产软件,而且许多这些产品被称为 ERP。 这些应用程序尽可能多地使用数据库,相信我,我知道一个 ERP 项目几乎在每个表单中使用这种控件,并且它有两到三百个表单。 为什么要使用它? 因为它允许您将数据源绑定到组合框,绑定意味着如果数据源发生更改,控件项目也会发生更改。 当选择一个项目时,可以从该项目获取数据行,最后获取数据行的主键。

关于控件

除了我之前提到的内容之外,此控件还有一个项目格式属性,用于根据数据行格式化项目。

假设我们有一个包含大量内容的表,其中包括员工姓名、年龄和工资... 我们希望以以下方式在控件中显示其项目:“Elvis Preasy, 收入 200 美元”。 行格式通过传递由 <>(类似于 HTML 标签)包围的列索引来实现。 因此,在前面的示例中,格式 string 将是 "<1> <2>, 收入 <3>$",假设列 1 和 2 定义了员工姓名,列 3 定义了他的收入。

而且这还不是全部,该控件支持行过滤,实际上它不会过滤其项目,而是根据过滤器重新创建新项目。 过滤器就像 ADO.NET 数据视图的行过滤器。

最后一点是可以通过获取与项目(选定项目)对应的数据行及其主键来实现。

这一切是如何实现的?

这一切是通过在控件中保存数据源(有些人想知道什么是数据源)、为我们提供项目的相应表、用于过滤的数据视图以及一些用于数据行关联的实体(意味着一行附带一个项目)来实现的。

// the association between all items and data rows
private ArrayList _bindedItems = new ArrayList();
// a data source object, not really used, but i needed 
private object _dataSource = null;
// the inner table that contains all the data binded to the control
private DataTable _controlDataTable = null;
// the inner data view that filters the inner table
private DataView _dataView = new DataView();
// a data member functionality
private string _dataMember = "";
// the display format for the items
private string _displayFormat = "";
// a row filter for the items
private string _rowFilter = "";

所有操作都是在调用控件属性时进行的

// format template : "some text <column number> some text <column number> ..."
public string BindedDataFormat [...]
// like ADO.NET data view row filter
public string BindedDataFiler [...]
public object BindedDataSource [...]
public string BindedDataMember [...] 
// get or set the selected item's data row
public DataRow SelectedRow [...]

有一些内部 private 函数可以处理所有数据,以及一个 static 过程,该过程返回对象向量中数据行的主键

public static object[] GetRowPK(DataRow row) 
{
    ArrayList pks = new ArrayList(); // make a new array list to hold the result
    DataTable dt = row.Table;        // get the data table associated with the row
    // and foreach data column witch is a primary key
    foreach(DataColumn dc in dt.PrimaryKey) 
    {
        pks.Add(row[dc]);            // add in the list its content
    }
    return pks.ToArray();            // finally return a object array
}

为了避免歧义和许多麻烦,控件的 DropDownStyle 属性是只读的。

为了使此控件绑定到数据源,我添加了数据表,该数据表提供了我们所需的所有行,以及在行更改和行删除时的处理过程。 在数据源更改时,旧表的处理方法将被删除。

// used to dynamically modify the control's items in case the data source changes 
private DataRowChangeEventHandler _rowChanged;
private void SetControlData()
{ 
   if(_controlDataTable == null) 
       return; 
   _controlDataTable.RowChanged += _rowChanged; 
   _controlDataTable.RowDeleted += _rowChanged;
private void ClearControlData() 
{ 
   if(_controlDataTable != null) 
   { 
       _controlDataTable.RowChanged -= _rowChanged; 
       _controlDataTable.RowDeleted -= _rowChanged; 
   }

在一个表单中,使用该控件非常容易……只需在其上附加一个格式(否则它将不显示任何内容)并设置其数据源即可

comboBox1.BindedDataFormat = "<id> <0>, <name> <1>, <surname> <2> ";
comboBox1.BindedDataSource = dataTable;

代替结束语

我希望有人觉得这个控件有用,因为我已经发现了...

数据绑定组合框 - CodeProject - 代码之家
© . All rights reserved.