綁定填充comobox控制項

來源:互聯網
上載者:User
控制項 #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;
}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。