Binding Fill Comobox Control

Source: Internet
Author: User
Control #region The bind fill Comobox control

public class Mycomboboxcontrol
{
#region "Custom Fields"

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 "Custom Properties"
<summary>
Error messages that may occur when populating
</summary>
public string Error
{
Get
{
Return terror;
}
}

<summary>
Combo controls can bind to data tables
</summary>
Public System.Data.DataTable souredatatable
{
Get
{
return tsourcedatatable;
}
Set
{
Tsourcedatatable=value;
}
}

<summary>
A collection of arrays of data to be saved as a combo data source
</summary>
Public System.Collections.ArrayList Sourearray
{
Get
{
return tsourcearray;
}
Set
{
Tsourcearray=value;
}
}

<summary>
The name of the index field provided by the Data source table
</summary>
public string Indexfieldname
{
Get
{
return tindexfieldname;
}
Set
{
Tindexfieldname=value;
}
}

<summary>
The name of the content field provided by the Data source table
</summary>
public string Valuefiledname
{
Get
{
return tvaluefieldname;
}
Set
{
Tvaluefieldname=value;
}
}

<summary>
The combobox control that will be bound
</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>
Populate the Data source table with the array set merge to 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>
The element class of the bound array collection
</summary>
<param name= "Pvalue" > Content, Value </param>
<param name= "Pindex" > Index, Key </param>
Public Mycomboboxitem (string pvalue, int pindex,params string[] pothervalue)
{
Tindex = Pindex;
TValue = pvalue;
Tothervalue=pothervalue;
}

#region "Custom Properties"

private string TValue;
private int tindex;
Private string[] Tothervalue;

<summary>
Content Properties
</summary>
public string Value
{
Get{return TValue;}
}

<summary>
Indexed properties
</summary>
public int Index
{
Get{return Tindex;}
}

<summary>
Secondary prompt properties
</summary>
Public string[] Othervalue
{
Get{return Tothervalue;}
}

#endregion

}
#endregion
Fills the specified number into the 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 "Populate the Affiliate station information to ComboBox"

public bool FILLSBDSTACBO (System.Windows.Forms.ComboBox Pcombobox)
{

DataSet t=new DataSet ();
Own database
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= "Populate the Affiliate station information failed!" "+m.error;
T=null;
M=null;
return false;
}

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.