#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;
}
