分享
 
 
 

绑定填充comobox控件

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有