绑定填充comobox控件

控件

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

时间: 2024-08-03 08:43:54

绑定填充comobox控件的相关文章

asp.net中将数据库绑定到DataList控件的实现方法与实例代码

解决方法1: datalist databind() 解决方法2: 查看MSDN上的详细说明资料 解决方法3: 在DataList的模板中用table表格,如: 复制代码 代码如下: <asp:DataList ID="dlDetailedInfo" runat="server" OnItemDataBound="dlDetailedInfo_ItemDataBound" Width="100%"> <Ite

asp.net Xml绑定到数据控件的具体实现

 这篇文章主要介绍了asp.net Xml绑定到数据控件的两种简单方法 ,需要的朋友可以参考下       方法一:aspx前台绑定 代码如下: <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/SaveFile/test2.xml"></asp:XmlDataSource> <asp:Repeater runat="serv

textbox-GridView绑定数据时控件有警告

问题描述 GridView绑定数据时控件有警告 各位大神,这错误提示个是什么原因啊 解决方案 http://blog.csdn.net/zhoufoxcn/article/details/1774872 在web.config中把 AJAX的标签改成别的就行了.如: <add tagPrefix=""asp"" namespace=""System.Web.UI"" assembly=""System

数据库中表绑定到treeview控件

问题描述 数据库中表绑定到treeview控件 如何在C++builder中把数据库"sql"中的一个表"tb_CarType"绑定到treeview控件上 以前没用过这些控件和数据库的知识,希望大虾们能给个详细的答案,谢谢 解决方案 QSqlRelationalTableModel了解下这个类的 然后就是MVC结构的用法

CheckedlistBox如何将数据库设置好的值绑定到该控件并置为checked

问题描述 CheckedlistBox如何将数据库设置好的值绑定到该控件并置为checked?我的思路是:1.先将数据库所有数据绑定起来,2.再读取数据库中设置好的值,放入DataTable.3.根据2.中的DataTable的值遍历并与1.中的值对比,如果相等就置为checked.现在的问题是第三步如何写?我没有找到控件方法,向大家请教了. 解决方案 解决方案二:把数据库中需要绑定的数据和设置好的值一次性读出来,然后在绑定checkedListBox的时候判断,处理解决方案三:第三部,两个遍历

winform 里CheckedlistBox如何将数据库设置好的值绑定到该控件并置为checked

问题描述 在Winform下,CheckedlistBox如何将数据库设置好的值绑定到该控件并置为checked?我的思路是:1.先将数据库所有数据绑定起来,2.再读取数据库中设置好的值,放入DataTable.3.根据2.中的DataTable的值遍历并与1.中的值对比,如果相等就置为checked.现在的问题是第三步如何写?我没有找到控件方法,向大家请教了. 解决方案 解决方案二:你是从数据库里面查询到的值然后绑定到checkedListBox上面吗,然后从另外一张表里面读取字段的值要和ch

asp.net中数据库绑定到DataList控件操作方法

我现在要做这个页面,然后我用DataList控件显示题目,我想问如何把我数据库教程的题目绑定到DataList控件里面,然后让它显示考试题目? 解决方法1: datalist databind() 解决方法2: 查看MSDN上的详细说明资料 解决方法3: 在DataList的模板中用table表格,如:<asp教程:DataList ID="dlDetailedInfo" runat="server" OnItemDataBound="dlDetai

webgrid里模板列绑定的画图控件怎么才能显示在WebCombo上

问题描述 各位高手好:小弟有个问题很困惑.Infragistics中WebCombo里绑定一个webgrid,webgrid里模板列绑定的画图控件怎么才能显示在WebCombo上.谢谢!!!我是刚来CSDN,手里分数不多,给大家的分也不多,还请理解. 解决方案 解决方案二:楼主找的是这个么??WebCombo.NET是一款基于AJAX技术,处于行业领先地位的Combobox控件.它提供众多高级的数据输入功能及其独特的技术.通过其内置的数据过滤功能,您可以在ASP.NET2.0Web应用程序中轻松

winform 文字绑定 用什么控件

问题描述 C#要实现绑定列表,如上图,用什么绑定控件啊~先拜过啦,希望大家多多指点啊 解决方案 解决方案二:顶起~请大家不吝赐教啊解决方案三:ListView解决方案四:列表一般都用DataGridView的解决方案五:上面的可以用lable显示就可以了解决方案六:Label解决方案七:用label就差不多了解决方案八:Label解决方案九:是在datagridview里插入lable?解决方案十:listview里插入label解决方案十一:用lable文字太长了怎么让他自动换行呢?解决方案十