绑定填充comobox控件

80酷酷网    80kuku.com

  控件#region "绑定填充comobox控件"

public class MyComboBoxControl
{
#region "自定义字段"

private string tIndexFieldName;
private string tValueFieldName;
private System.Collections.ArrayList tSourceArray;
private System.Windows.Forms.ComboBox tComboBox;
private System.Data.DataTable tSourceDataTable;
private string tError="";
private string[] tOtherField;

#endregion

#region "自定义属性"
/// <summary>
///填充时可能发生的错误信息
/// </summary>
public string error
{
get
{
return tError;
}
}

/// <summary>
/// Combo控件可以绑定的数据表
/// </summary>
public System.Data.DataTable SoureDataTable
{
get
{
return tSourceDataTable;
}
set
{
tSourceDataTable=value;
}
}

/// <summary>
/// 保存数据的数组集合,将做为combo的数据源
/// </summary>
public System.Collections.ArrayList SoureArray
{
get
{
return tSourceArray;
}
set
{
tSourceArray=value;
}
}

/// <summary>
///数据源表提供的索引字段名称
/// </summary>
public string IndexFieldName
{
get
{
return tIndexFieldName;
}
set
{
tIndexFieldName=value;
}
}

/// <summary>
/// 数据源表提供的内容字段名称
/// </summary>
public string ValueFiledName
{
get
{
return tValueFieldName;
}
set
{
tValueFieldName=value;
}
}

/// <summary>
/// 将绑定的ComboBox控件
/// </summary>
public System.Windows.Forms.ComboBox ComboBox
{
get
{
return tComboBox;
}
set
{
tComboBox=value;
}
}
#endregion

public MyComboBoxControl()
{
tSourceArray=new System.Collections.ArrayList();
tComboBox=new System.Windows.Forms.ComboBox();
}


public MyComboBoxControl(System.Data.DataTable pDataTable)
{
tSourceDataTable=pDataTable;
tSourceArray=new System.Collections.ArrayList();
tComboBox=new System.Windows.Forms.ComboBox();
}


public MyComboBoxControl(System.Windows.Forms.ComboBox pComboBox,
System.Data.DataTable pDataTable,
string pValueFieldName,
string pIndexFieldName,
params string[] pOtherField)
{
tSourceDataTable=pDataTable;
tSourceArray=new System.Collections.ArrayList();
//pComboBox.Items.Clear();
tComboBox=pComboBox;
tValueFieldName=pValueFieldName;
tIndexFieldName=pIndexFieldName;
tOtherField=pOtherField;

BindComboBoxByDataTable();
}

/// <summary>
/// 将数据源表填入数组集合并绑定到combobox
/// </summary>
/// <returns></returns>
public bool BindComboBoxByDataTable()
{
try
{
foreach(DataRow tDataRow in tSourceDataTable.Rows)
{
string[] m=null;//=new string[tOtherField.Length];

if (tOtherField!=null)
{
if (tOtherField.Length>0)
{
m=new string[tOtherField.Length];
for(int i=0;i<=tOtherField.Length-1;i++)
{
m[i]=tDataRow[tOtherField[i]].ToString();
}
}
}

tSourceArray.Add(new MyComboBoxItem(tDataRow[tValueFieldName].ToString() ,
Convert.ToInt32(tDataRow[tIndexFieldName]),m));
}

if (tSourceDataTable.Rows.Count >0)
{
tComboBox.DataSource = tSourceArray;
tComboBox.DisplayMember ="Value";
tComboBox.ValueMember = "Index";
}

return true;
}
catch (System.Exception e)
{
tError=e.ToString();
return false;
}
}
}

public class MyComboBoxItem
{

public MyComboBoxItem()
{

}

/// <summary>
/// 绑定的数组集合的元素类
/// </summary>
/// <param name="pValue">内容、值</param>
/// <param name="pIndex">索引、键</param>
public MyComboBoxItem(string pValue, int pIndex,params string[] pOtherValue)
{
tIndex = pIndex;
tValue = pValue;
tOtherValue=pOtherValue;
}

#region "自定义属性"

private string tValue;
private int tIndex;
private string[] tOtherValue;

/// <summary>
/// 内容属性
/// </summary>
public string Value
{
get{return tValue;}
}

/// <summary>
/// 索引属性
/// </summary>
public int Index
{
get{return tIndex;}
}

/// <summary>
/// 辅助提示属性
/// </summary>
public string[] OtherValue
{
get{return tOtherValue;}
}

#endregion

}
#endregion
//填充指定数值到ComboBox中
public void FillDataInfoToCombo(System.Windows.Forms.ComboBox pComboBox,string pName,int pId,params string[] pOther)
{
My.MyComboBoxControl t=new TRAFFICERPSYSTEM.My.MyComboBoxControl();
My.MyComboBoxItem m=new TRAFFICERPSYSTEM.My.MyComboBoxItem(pName,pId,pOther);

t.SoureArray.Add(m);
pComboBox.DataSource=t.SoureArray;
pComboBox.DisplayMember ="Value";
pComboBox.ValueMember = "Index";
t=null;
}

#region "填充隶属站信息到combobox"

public bool FillSbdStaCbo(System.Windows.Forms.ComboBox pComboBox)
{

DataSet t=new DataSet ();
//自己连数据库
Business.SysManage.SysManage s=new Business.SysManage.SysManage();

if (!s.GetSubordStation(t,"SbdStaInfo"))
{
tError=s.err;
t=null;
s=null;
return false;
}

My.MyComboBoxControl m=new TRAFFICERPSYSTEM.My.MyComboBoxControl(pComboBox,t.Tables[0],"SbdStaName","SbdStaId");
if (m.error!="")
{
tError="填充隶属站信息失败!"+m.error;
t=null;
m=null;
return false;
}

m=null;s=null;t=null;
return true;
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: